During an unattended installation with WDS, I need to run a script on a share just at the end of pass 7 oobeSystem.

I use the FirstLogonCommands / SynchronousCommand option and provide this command line:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -WindowStyle Maximized -NoProfile -Command {New-SmbMapping -LocalPath z: -RemotePath \\\Share -Persistent $false -UserName user -Password password; z:\script.ps1}

It opens the PowerShell console, but displays the scriptblock at the top of this window before displaying the command prompt, and unfortunately does not execute the scriptblock.

I tried to introduce a sleep before, but it doesn't change the result. Here is an example:


How can I fix this?

  • 1
    From powershell -?: To write a string that runs a Windows PowerShell command, use the format: "& {<command>}"
    – JosefZ
    Aug 10, 2019 at 12:15
  • I already tried that but I will try again. Give some time...
    – Luke
    Aug 10, 2019 at 12:39
  • Please note that angle brackets < and > (or Less-Than Sign and Greater-Than Sign) here are metasymbols, not a part of resultant code. Hence, you can use something like the following: … -Command "& {Start-Sleep 30; Get-Childitem .}".
    – JosefZ
    Aug 10, 2019 at 13:45
  • It appears that "& {Get-Childitem -Path C:\}" works fine, but "& {New-SmbMapping -LocalPath z: -RemotePath \\\Share; z:\script.ps1}"doesn't even display a PowerShell console at all...
    – Luke
    Aug 10, 2019 at 16:52
  • My bad for the former partial copy/paste because I though some part was irrelevant... "& {New-SmbMapping -LocalPath z: -RemotePath \\\Share -UserName User -Password password}" worsk fine. However, "& {New-SmbMapping -LocalPath z: -RemotePath \\\Share -Persistent $false -UserName User -Password password}" does not open any PowerShell window. Maybe the $ sign is the problem here... Trying some more dichotomy...
    – Luke
    Aug 10, 2019 at 18:53

1 Answer 1


The FirstLogonCommands documentation shows an XML Example how to specify two commands to run after first logon. Modifying CommandLine according to your circumstances, relevant part of an unattend.xml could look as follows:

   <SynchronousCommand wcm:action="add">
      <CommandLine>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command "& {New-SmbMapping -LocalPath z: -RemotePath \\\Share -UserName User -Password password -Persistent 0}"</CommandLine>
   <SynchronousCommand wcm:action="add">
      <CommandLine>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -File z:\script.ps1</CommandLine>

Please note that in above code snippet the -NoExit parameter is removed completely and the -WindowStyle Maximized is changed to -WindowStyle Hidden, according to your statement from our previous discussion: "I use the -NoExit parameter just to be sure if the command fails I can see what happened".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.