0

I am trying to create a PowerShell command that reads a registy variable and then executes a command. It does this from the command line using CMD and it runs under SYSTEM.

I have the following code:

powershell.exe -ExecutionPolicy ByPass -WindowStyle Minimized -Command 
"$ErrorActionPreference = 'silentlycontinue' |`
 New-Variable -name UserNameForSapFolderRemoval -Value (get-itemproperty 'HKCU:\Volatile Environment' |`
 Select-Object -ExpandProperty UserName) |`
 Remove-Item $("C:\Users\"+$UserNameForSapFolderRemoval +"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Sapgui 7.30") -Force -Recurse |`
 Remove-Variable -Name UserNameForSapFolderRemoval"

But it returns with:

The string is missing the terminator

I added the " character but without succes.

Any idea how I can get this powershell command to run succesful?

1 Answer 1

1

Parsing username from registry is way too complicated, as there already is $env:username. Try something like this,

powershell -command { rm -force -recurse "c:\users\$env:username\appdata\whatever" }
Sign up to request clarification or add additional context in comments.

2 Comments

I use that because the script runs under SYSTEM. I tried the { and } character but without success.
@user2955447 It would make sense to include such a bit of info in the question.

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.