Home > Blockchain >  Powershell: Remove-Item - path throwing error at integers
Powershell: Remove-Item - path throwing error at integers

Time:07-12

This is a relatively short one but cannot seem to figure out how the computer is able to see this differently. I have a folder I want to delete with Powershell but the Remove-Item function is throwing an error because the path contains Program Files (x86).

See below code:

Invoke-Command -ComputerName $computer {Remove-Item -Path C:\Program Files (x86)\Microsoft\Test -Force -Verbose}

It doesn't like the fact that the program files has brackets around the x86. When I put quotation marks around the path it doesn't recognise it at all.

How can I get around this?

Error even when using single quotes:

Invoke-Command -ComputerName $computer {Remove-Item -Path 'C:\Program Files (x86)\Microsoft\Test' -Force -Verbose}

Cannot find path 'C:\Program Files (x86)\Microsoft\Test' because it does not exist. CategoryInfo : ObjectNotFound: (C:\Program File...test:String) [Remove-Item], ItemNotFoundException FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand PSComputerName : MKC-03812

CodePudding user response:

This might help some https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.2

but the answer is

Invoke-Command -ComputerName $computer {Remove-Item -Path 'C:\Program Files (x86)\Microsoft\Test' -Force -Verbose}

CodePudding user response:

Your issue is using double quotes. Use single quotes to stop the interpretation of the parenthesis as its own clause/statement.

Invoke-Command -ComputerName $computer {Remove-Item -Path 'C:\Program Files (x86)\Microsoft\Test' -Force -Verbose}

Also, if your commands contains complex or nested quoting, you may have more luck using the -LiteralPath parameter instead.

Invoke-Command -ComputerName $computer {Remove-Item -LiteralPath 'C:\Program Files (x86)\Microsoft\Test' -Force -Verbose}
  • Related