RalJans
RalJans

Reputation: 154

Using the -command PowerShell feature does not executing in CMD

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?

Upvotes: 0

Views: 179

Answers (1)

vonPryz
vonPryz

Reputation: 24071

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" }

Upvotes: 1

Related Questions