Running PowerShell scripts
It is now time to run your first script!
Through shell or through ISE
You can run ad hoc commands through the shell or through the Integrated Scripting Environment (ISE).
To use the PowerShell console, you can launch the shell by opening Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell. Often when managing your servers, you may need to run this as Administrator (right-click on the PowerShell icon and select Run as Administrator).
Once the console is ready, you can type your commands and press Enter to see the results. For example, to display ten (10) running processes, you can use the Get-Process
cmdlet, as shown in the following screenshot:
You can also use the ISE, and to launch the ISE, go to Start | All Programs | Accessories | Windows PowerShell | Windows PowerShell ISE. Similar to the shell, you can type your command and press the Run button (green arrow icon).
Note
More details about the ISE are covered in Chapter 1, Getting Started with SQL Server and PowerShell.
Typically, you
would save your commands in a script file with the .ps1
extension, and run them from the shell in few different ways:
From PowerShell console, using the call operator (
&
):PS C:\ > & "C:\PowerShell\My Script.ps1"
From PowerShell console, using dot sourcing. Dot sourcing simply means you prepend a dot and space to your invocation. You would invoke your script by using dot sourcing to persist variables and functions in your session:
PS C:\PowerShell > . ".\My Script.ps1" PS C:\> . "C:\PowerShell\My Script.ps1"
From a command prompt:
C:\>powershell.exe -ExecutionPolicy RemoteSigned -File "C:\PowerShell\My Script.ps1"
Execution policy
PowerShell scripts are not authorized to just run.
Remember the "I Love You" virus? It took off because it was so easy to launch a script just by double-clicking the .vbs
file.
To avoid problems such as this, PowerShell scripts by default are blocked from running. This means you cannot just accidentally double-click a PowerShell script and execute it.
The rules that determine which PowerShell scripts can run are contained in the Execution Policy. This will need to be set ahead of time. The different settings are:
Execution Policy |
Description |
---|---|
|
Default execution policy PowerShell will not run any scripts |
|
PowerShell will run only signed scripts |
|
PowerShell will run signed scripts, or locally created scripts |
|
PowerShell will run any scripts, signed or not |
|
PowerShell will not block any scripts, and will prevent any prompts or warnings |
|
PowerShell will remove set execution policy in current user scope |
To determine what your current setting is, you can use
Get-ExecutionPolicy
:
PS C:\>Get-ExecutionPolicy
If you try to run a script without setting the proper execution policy, you may get an error similar to this:
File C:\Sample Script.ps1 cannot be loaded because the execution of scripts is disabled on this system. For more information, see about_execution_policies.
To change the execution policy, use
Set-ExecutionPolicy
:
PS C:\>Set-ExecutionPolicy RemoteSigned
Typically, if you need to run a script that does a lot of administrative tasks, you will need to run the script as administrator.
To learn more about execution policies, run:
help about_execution_policies
For more information about how to sign your script, use:
help about_signing