i am writing a Programm where the script has to change the location to the AppData Folder. But the User at the PC is changeing so there is in my optinon no way to do this static so i wrote a script which picks out the user which is logged in. The Problem is i can´t use this variable in an cd oder Set-Location. Is there a workaround or even a better way to do that. Here is what i got so far:
$output = Get-WmiObject Win32_ComputerSystem -ComputerName "localhost" | Select-Object UserName
$temp= Out-String -InputObject $output -Width 100
$temp.GetType()
$temp1 = $temp.Split("\")[1] ##The Output of $temp1 is domain\username, therefore the split
$temp1
Set-Location "C:\Users" $temp1
Thanks mojo
CodePudding user response:
You should switch to the CIM cmdlets, they did replace the WMI cmdlets. Also you could do the filtering at CIM level:
#Query UserName, replace domain\
$userName = (get-cimInstance -query "select username from Win32_ComputerSystem").username -replace '.*\\'
Set-Location ('C:\Users\' $username)