Error running `Invoke-Command`



  • 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 {}

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2