need help with add-content

Welcome Forums General PowerShell Q&A need help with add-content

Viewing 3 reply threads
  • Author
    • #195425
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      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 '.

    • #195440
      Topics: 3
      Replies: 169
      Points: 891
      Helping Hand
      Rank: Major Contributor

      Hi Rahul,

      Try this...

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

      Thank you.

    • #195479
      Topics: 27
      Replies: 739
      Points: 2,015
      Helping Hand
      Rank: Community Hero

      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.

    • #195482
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Thanks Kiran, using your suggestion I ran into the same "positional error"

      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.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.