PowerShell Invoke-Expression command-line option contains a comma

What to do when the password contains special characters PS?

Invoke-Expression -Command "net use x: $Path /USER:domain1\user1 7Ui4RT,@T /persistent:no" 

This doesn't match the syntax - because PS interprets 7Ui4RT,@T as an array

 Invoke-Expression -Command "net use x: $Path /USER:domain1\user1 7Ui4RT`,@T /persistent:no" 

This does not match the syntax - apparently because PS cannot interpret 7Ui4RT``,@T

 Invoke-Expression -Command "net use x: $Path /USER:domain1\user1 "7Ui4RT,@T" /persistent:no" 

This fails because PS interprets 7Ui4RT,@T as an object, not a string (error = "A positional parameter that takes the argument" System.Object [] "cannot be found.)

What to do?

+4
source share
2 answers

Why are you using Invoke-Expression to evaluate your team? If you need to dynamically create a set of arguments, you can put them into an array:

 $command = 'net' $commandArgs = @('use','x:',$Path,'/USER:domain1\usr1','7Ui4RT,@T','/persistent:no') & $command $commandArgs 

If you know the command ahead of time, you can call it directly: net $commandArgs

+5
source

Put single quotes around 7Ui4RT,@T , i.e.

 '7Ui4RT,@T' 
+2
source

All Articles