I am trying to run the below command in powershell

Invoke-Command -Computer 'someComputer' ScriptBlock {Remove-Item $args -Force -Recurse} -ArgumentList 'C:\Users\someUser\AppData\Local\Temp'

but getting the below exception.

Invoke-Command : A positional parameter cannot be found that accepts argument 'Remove-Item $args -Force -Recurse'. At line:1 char:1 + Invoke-Command -Computer 'someComputer' ScriptBlock {Remove-Item $arg ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

Any idea what am I missing here?


Try the below command:

Invoke-Command -ComputerName  'someComputer' -ScriptBlock {Remove-Item $args -Force -Recurse} -ArgumentList {'C:\Users\someUser\AppData\Local\Temp'}
  1. Invoke command does not have '-computer', it is -ComputerName ,

  2. scriptblock is also a command-line option so should be passed as -ScriptBlock

  3. -Argument list expects a list, so should be passed inside {}
| improve this answer | |
  • 1
    Thanks. I was missing two things -ComputerName instead of -Computer and - before the ScriptBlock. – Akash Bhatia Nov 30 '19 at 9:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.