Wednesday, 19 October 2016

Powershell and 'net use' with ampersand

So I had to make a script to transfer some files over the network using Robocopy.  And the password has an ampersand in it, so Robocopy parsed the password into a new command. D'oh!

Some documents say prepend the & with ^ (^&). Some say surround in double quotes ("&"). The error message Powershell gives you is to wrap in double quotes. It even shows you an example! ("&")

But it is wrong. In Powershell you have to wrap the ampersand in single quotes ('&').

 net use \\TO_DOMAIN_OR_IP\g$ /user:TODOMAIN\TOUSER pass'&'word
 robocopy c:\backups\folder\ \\TO_DOMAIN_OR_IP\c$\backups\ /Z /MIR

Good luck!

No comments:

Post a Comment