need help with add-content

Hi there,

I am a very rookie in powershell, so pardon me if I am doing totally wrong. I have tried to understand it but its off my head,

I am trying to get this command to work to incorporate into my AWS provisioiner.

Add-Content C:\Windows\Setup\Scripts\SetupComplete.cmd “Powershell.exe -noexit -ExecutionPolicy Bypass “& ‘C:\users\administratordesktop\configure.ps1’””

I get

The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part
of a string."

If I wrap the ampersand in quotes I get

 

Add-Content : A positional parameter cannot be found that accepts argument 'C:\Windows\Setup\Scripts\SetupComplete.cmd '.

Hi Rahul,

Try this…

[pre] Add-Content -Path C:\Windows\Setup\Scripts\SetupComplete.cmd -Value ‘powershell.exe -noexit -ExecutionPolicy Bypass "& ’ ’ C:\users\administratordesktop\configure.ps1’ ’ "’

[/pre]

Thank you.

You’re trying to put one set of doublequotes inside another. But powershell thinks it’s two strings instead of one. You can escape the inner doublequotes with a backquote. Your trouble with “&” is that it is interpreted by cmd (run the 2nd command if the first command is true).

Add-Content C:\Windows\Setup\Scripts\SetupComplete.cmd "Powershell.exe -noexit -ExecutionPolicy Bypass `"& 'C:\users\administratordesktop\configure.ps1'`""

But the & and quotes can be taken out.

Add-Content C:\Windows\Setup\Scripts\SetupComplete.cmd "Powershell.exe -noexit -ExecutionPolicy Bypass C:\users\administratordesktop\configure.ps1"

I’m not sure why you need the -noexit.

[quote quote=195440][/quote]
Thanks Kiran, using your suggestion I ran into the same “positional error”

[quote quote=195479][/quote]

JS, the tilda did the trick for me, its coming out as how I wanted now.

 

Thank you so much, I really appreciate your help on this.