# How do I run Powershell or Batch scripts (locally) from a Task Sequence?

• ### Question

• Hi All,

I am new to MDT 2013 and trying to fully automate my build of Windows 10 (or as much as possible).  To be clear I can't use GPOs since this machine is not networked joined since its going to act as my gold image. Once complete I'll add the automated step to capture back as a WIM and then its completely hands off.  Windows 10 16whatever comes out, I point the TS to that base OS ISO and just re-run since all the configuration is already captures.

To do this -- Essentially I have a TS that creates the base Windows 10 build, but then I have several powershell and command line scripts I want to run after the OS is built.  Where *exactly* do I put this in the Task Sequence sequence, and how do I *exactly* call the scripts?  Part of my issue maybe how I am trying to do this so bear with me.

After the OS is built I have a script to copy my scripts and related files from \$OEM\$\\$1 to C:\RUNONCE.  In this directory are installation files for Office 2016, Language Packs, etc.  This works perfect.  Its when I try to execute the scripts in C:\RUNONCE from the TS that fails.

For example, What I am *trying* to do is have the task sequence run setup.exe for Office 2016 from the C:\RUNONCE directory instead of installing it over the network from the %DEPLOYMENTSHARE%.  I dont know how to run a local command-line from a TS such as this:

C:\RUNONCE\OFFICE_2016_MUI\setup.exe /config c:\RUNONCE\office_2016_MUI\proplus.ww\config.xml

or

Powershell –executionpolicy bypass "& 'c:\RUNONCE\win10regKeys.ps1'"

I've tried using cmd /c in front but my sequence always fails at that step.  But I also don't know would this step go post-installation?  After OS install?

What is the best way to have this stuff run locally from C: drive in a task sequence?  Or is that not possible and I am restricted to installing from the network deployment share

Appreciate it

Christopher Westpoint - Infrastructure Architect

Tuesday, January 10, 2017 9:06 PM

• I figured out my own answer.  Sharing here in case others run into the same issue.

If you want to add powershell or a command line to the task sequence, place the task after state restore but before imaging section.  I created a group for all of mine, but it is not necessary.

For each task, create a "General | Run Commandline"

For the command (which should be stored in the Deploymentshare\scripts directory) the syntax is:

cmd /c <c:\windows\system32>\netsh.exe <command>

where c:\windows\system32 is the full path to the executable you want to run (this is what I was missing)

For powershell it is slightly different, since I am calling powershell from a command line in a bach

cmd /c %WinDir%\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass "& <full path to PS1 script>.ps1"

Since I had so many and I wanted to test each script/batch individually I modified the beginning of each batch to

start /wait cmd.exe /k <batch or powershell line>

Doing that brought up a cmd window, ran the script, and stops, until I exit out of the cmd window, where the next one pops up.  This allowed me to troubleshoot any issues.  Once that worked without issue put the commands back to their original state and could run silently with confidence.

No I have a fully automated Win 10 core build.

Hope this helps.

Christopher Westpoint - Infrastructure Architect

Wednesday, January 11, 2017 8:03 PM

### All replies

• I figured out my own answer.  Sharing here in case others run into the same issue.

If you want to add powershell or a command line to the task sequence, place the task after state restore but before imaging section.  I created a group for all of mine, but it is not necessary.

For each task, create a "General | Run Commandline"

For the command (which should be stored in the Deploymentshare\scripts directory) the syntax is:

cmd /c <c:\windows\system32>\netsh.exe <command>

where c:\windows\system32 is the full path to the executable you want to run (this is what I was missing)

For powershell it is slightly different, since I am calling powershell from a command line in a bach

cmd /c %WinDir%\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass "& <full path to PS1 script>.ps1"

Since I had so many and I wanted to test each script/batch individually I modified the beginning of each batch to

start /wait cmd.exe /k <batch or powershell line>

Doing that brought up a cmd window, ran the script, and stops, until I exit out of the cmd window, where the next one pops up.  This allowed me to troubleshoot any issues.  Once that worked without issue put the commands back to their original state and could run silently with confidence.

No I have a fully automated Win 10 core build.

Hope this helps.

Christopher Westpoint - Infrastructure Architect

Wednesday, January 11, 2017 8:03 PM
• MDT has Run Powershell steps that already do what you want for PS.

Many questions such as where do I find logs and what logs are interesting are found in: MDT TechNet Forum - FAQ & Getting Started Guide Please take the time to read it. Also if you don't post logs your problem won't be easily solved.

Wednesday, January 11, 2017 8:53 PM