0

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

1
  • Edit: The script is started as system32 so i cant use the Envvariable Username Commented Oct 12, 2022 at 6:05

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.