Running powershell scripts as part of a Task Sequence

By | January 16, 2012

I’ve seen various ways of running powershell scripts as part of a Task Sequence some that have worked well some not so well.  The method that I have had 100% success rate with is

powershell.exe -command “set-ExecutionPolicy Unrestricted; cpi %SCRIPTROOT%\scriptname.ps1 -destination c:\; c:\scriptname.ps1; ri c:\*.ps1; set-ExecutionPolicy restricted”

This command basically drops the excution policy to unrestricted, copies the script to the local c:\, executes the script, removes the script and then sets the excution policy back to restricted.

The above assumes that you are using MDT or SCCM with MDT integrated.

If you are just using SCCM with no MDT integration, all you need to do is create a new package which contains your script and then add a step in your Task Sequence to run a command line, selecting the Package that contains your script.

Your command line should then look like:

powershell.exe -command “set-ExecutionPolicy Unrestricted; cpi scriptname.ps1 -destination c:\; c:\scriptname.ps1; ri c:\*.ps1; set-ExecutionPolicy restricted”

5 thoughts on “Running powershell scripts as part of a Task Sequence

  1. cwaite

    Where does the script need to live on the SCCM DP? How to define %SCRIPTROOT%?

    Reply
    1. admin Post author

      If you are just using SCCM with no MDT integration, create a new package which contains your script and then add a step in your Task Sequence to run a command line and select the Package that contains your script. You can then drop the the %scriptroot%\ from your command line.

      If you have MDT integrated with SCCM the script should be placed in the scripts directory within your MDT Deployment share and then the relevant packages within SCCM updated including the SCCM DP.

      I’ll update my post so it’s a bit clearer.

      Reply
    1. Evan

      Hopefully this thread is still monitored. I came across this thread and tried the script in my task sequence and it doesn’t seem to even try to run. It just errors and kills the deployment. I have a package which contains my scripts and i have tested the script manually and it works. I have your command line above mirrored for my script. I am referencing the script package I create in the command line step in the TS. I am not setting an account to run the step in the TS nor am I specifying a start in directory.

      Here is my command line: powershell.exe -command “set-ExecutionPolicy Unrestricted; cpi RenameTerminal.ps1 -destination c:\; c:\RenameTerminal.ps1; ri c:\*.ps1; set-ExecutionPolicy restricted”

      I am running this on POSReady 7….which shouldn’t differ much from Win7. Your command line should work for PS v1 correct?

      Reply
      1. admin Post author

        Quickest way to test is to run the command within the OS before adding it to a TS. I’m just about to put a post up to run a debug task sequence this might help with your testing.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *