Getting Started with PowerShell
I’m currently setting up a Microsoft System Center Virtual Machine Manager (SCVMM) environment. I can see that there are quite a few things that I’d like to script (starting and stopping VMs, creating checkpoints etc) so it’s time to start playing around with PowerShell.
One of the first things that I noticed was that I wasn’t able to run .ps1 scripts due to the execution policy. By default, only interactive commands can be run in PowerShell; all scripted commands are restricted. You can however change this to one of these settings:
Restricted
- Default execution policy.
- Does not run scripts.
- Interactive only.
AllSigned
- Runs scripts.
- All must be signed by a publisher that you trust.
RemoteSigned
- Runs scripts.
- Downloaded files must be signed by a publisher that you trust.
Unrestricted
- Runs scripts.
- No digital signature is required.
(note that I pasted the settings from the help text that generated by running Get-Help Set-ExecutionPolicy)
To get started, I set it to RemoteSigned by running the following command:
set-executionpolicy RemoteSigned
Once I set the execution policy I was able to run .ps1 files. To run a .ps1 file, open notepad and paste in a powershell command, for example write-host "test", then save the file with a .ps1 extention, for example test.ps1. To run the script, open PowerShell and type in the full path name of the file. Note that you can ommit the extension:
PS C:\> c:\scripts\test
test
If you are already in the correct directory (which you can change by using cd\ c:\scripts then you can just issue .\test:
PS C:\Scripts> .\test
test
So that’s it. I’ll post some more examples specific to SCVMM once I’ve got some scripts working.