Skip navigation
laptop_typing.jpg Getty Images

How To Run PowerShell Scripts

Want to run a PowerShell script? Here are the steps to follow.

PowerShell scripts have a wide variety of purposes. Microsoft, for example, sometimes provides PowerShell scripts to automate difficult configuration tasks. Similarly, organizations often run PowerShell scripts to automate the process of onboarding new users. Such a script might create a user’s Active Directory account and add the user to a group. In my own organization, I use PowerShell scripts to monitor disk health.

So, seeing that PowerShell scripts can be unquestionably useful, how do you run them?

A Word of Caution

Before I explain how to run a PowerShell script, I need to give a quick word of caution. When you execute a PowerShell script, that script runs with the same permissions as the account you are currently logged in with.

Depending on what a script is designed to do, you may sometimes need to run the script with administrative privileges. However, it’s best to avoid using a privileged account if possible. This is especially true if you downloaded the script from the internet. If you happened to download a malicious script, its ability to inflict damage on your system will be limited by your account’s permissions.

Use PowerShell in Administrative Mode

Even if you are logged in as an administrator, PowerShell does not automatically allow you to exercise your administrative authority. If you need to run a PowerShell script as an administrator, you will need to open PowerShell in administrative mode.

To do so, find PowerShell on the Start menu, right click on the PowerShell icon, and then select More | Run as Administrator from the shortcut menu. You can see what this looks like in Figure 1.

Brien PoseyScreenshot of PowerShell on the Start menu

Figure 1. Right click on the PowerShell icon and choose the More | Run as Administrator commands from the shortcut menus.

Note that using the Run as Administrator option is not a requirement for running all PowerShell scripts. Using the Run as Administrator option is only necessary if the script requires administrative privileges.

Disable the Execution Policy

Here’s the next thing you need to know about running PowerShell scripts. Depending on what version of Windows you have, where you got the script, and how your system is configured, the system’s execution policy may prevent you from running the PowerShell script.

Thankfully, it’s relatively easy to temporarily disable the execution policy so that your script can run.

Execution policies are designed to prevent you from accidentally running a potentially untrustworthy PowerShell script. If you need to disable an execution policy to run a script, the easiest way to do that is to type this command:

Set-ExecutionPolicy Unrestricted

When you’re done running the script, it’s a good idea to reenable the execution policy. There are several different types of execution policies, but the most secure one is called restricted. You can enable it by running this command:

Set-ExecutionPolicy Restricted

Running the Script

With the execution policy out of the way, you can now run your script.

First, navigate to the folder containing the script. In PowerShell, the CD command is used for navigating the directory structure. Suppose for a moment that right now the PowerShell prompt is pointing to the C:\Data folder and I need to run a script located in C:\Scripts. I could navigate to that folder by typing these commands:

CD..

CD Scripts

The first command drops down a level in the directory hierarchy. The second command goes to the specified folder name. However, if the folder name consists of more than a single word, then you will need to put the folder name in quotation marks to avoid receiving an error. For example, if the script is in a folder named My Scripts (instead of just Scripts), then you would use this command:

CD “My Scripts”

Once you have navigated to the folder containing the script, you can run the script in question. To do so, just type a period, followed by a slash and the filename. Don’t forget to include the .PS1 extension.

If you look at Figure 2, you can see that the first command that I typed was CD\. This is a shortcut. The CD.. command drops down one level in the folder hierarchy. Since I needed to go all the way to the root directory, I used CD\ rather than typing CD.. twice. The next thing that I did was to run a script called HelloWorld.ps1 by typing ./HelloWorld.ps1.

Brien PoseyScreenshot of a PowerShell session

Figure 2. Place ./ in front of the file name to run the script.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish