infinit_e
infinit_e

Reputation: 57

PowerShell externall command failing

As part of a project I'm working on I need to get a couple files copied from one user's desktop to each new user that logs into a computer. I'm running the following command via a desktop environment manger.

%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -command "get-childitem -path $env:systemdrive\Users\Administrator\Desktop\* | Where-Object {$_.Name -like "*NAME*"} | Copy-Item -Destination $env:userprofile\Desktop"

Running that command by itself from within PowerShell works fine, but when I'm passing it externally it errors out.

At line:1 char:100
+ ...  {$_.Name -like *NAME*} | Copy-Item -Destination
$env:userprofile\Desktop\}
+                    ~
You must provide a value expression following the '-like' operator.
At line:1 char:102
+ ... $_.Name -like *NAME*} | Copy-Item -Destination
$env:userprofile\Desktop\}
+                    ~
You must provide a value expression following the '*' operator.
At line:1 char:102
+ ... $_.Name -like *NAME*} | Copy-Item -Destination
$env:userprofile\Desktop\}
+                    ~~~~~~~~~~
Unexpected token 'NAME*' in expression or statement.
+ CategoryInfo          : ParserError: (:) [],      
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression

I'm not sure what I need to adjust in my formatting to resolve this, or even maybe there's even a better way to do this. Any input would be greatly appreciated.

Upvotes: 0

Views: 38

Answers (1)

infinit_e
infinit_e

Reputation: 57

Per Mathias suggestion adding the \ prior to the " resolved the errors.

Upvotes: 1

Related Questions