Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Invoke-Command Remove-Item Remote Server

Tags:

powershell

I want to delete files on a particular folder of a remote server. I execute the following in Powershell:

Invoke-Command -Computer 'compname1' -ScriptBlock {Remove-Item -$args -force } -ArgumentList 'c:\BizTalkDeployment'

I get the following error:

Cannot find drive. A drive with the name '-c' does not exist.

c:\BizTalkDeployment is the correct path on server compname1.

Can anyone please explain what I've done wrong?

like image 640
Rob Bowman Avatar asked Jul 30 '12 16:07

Rob Bowman


2 Answers

Remove the - in front of $args and re-run the script. You might even try changing the script block to:

Remove-Item -Path $args[0]
like image 51
Chris N Avatar answered Oct 12 '22 02:10

Chris N


You could also remove the folder using a UNC path:

Remove-Item \\compname1\c$\BizTalkDeployment -force
like image 44
Shay Levy Avatar answered Oct 12 '22 02:10

Shay Levy