PowerShell is this awesome tool that Microsoft is integrating to just about everything. Back to the command line we go. Scripting is really powerful and can really streamline a lot of processes.

One of most common questions I get is, “How do I run a script in PowerShell?”

This might seem really simple until you actually go a run a script.

In the following screen shot I am trying to run the script getver.ps1 which is in the directory ‘c:\temp\scripts’. Since PowerShell looks similar to the command shell you would think that you could just type the name of the script. When you do you get the first response. In PowerShell you have to type the whole path of the script, or if you are in the directory already you can use ‘.\scriptname to call the script. However, you will notice that that doesn’t work either.

Before you can run a script you have to set the execution policy. There are 4 levels.

  • Restricted – No scripts can be run. Windows PowerShell can be used only in interactive mode.
  • AllSigned – Only scripts signed by a trusted publisher can be run.
  • RemoteSigned – Downloaded scripts must be signed by a trusted publisher before they can be run.
  • Unrestricted – No restrictions; all Windows PowerShell scripts can be run

The default is Restricted. You can set your policy to AllSigned or RemoteSigned which works great in your environment to distribute scripts. If you are just testing and developing scripts you may want to use Unrestricted on that workstation. For a more information on script signing check out this article http://technet.microsoft.com/en-us/magazine/2008.04.powershell.aspx

As you can see I set the execution policy to Unrestricted and then ran the script. The script does run at this point at returns the version of the OS that is running.

Hopefully, this will help get those scripts running.

Share
© 2010 T3 Training Suffusion WordPress theme by Sayontan Sinha