24/7/365 Support

Write a Script in PowerShell

Problem

You want to store your commands in a script, so that you can share them or reuse them later.

Solution

To write a PowerShell script, create a plaintext file with your editor of choice. Add your PowerShell commands to that script (the same PowerShell commands you use from the interactive shell) and then save it with a .ps1 extension.

Discussion

One of the most important things to remember about PowerShell is that running scripts and working at the command line are essentially equivalent operations. If you see it in a script, you can type it or paste it at the command line. If you typed it on the command line, you can paste it into a text file and call it a script.

Once you write your script, PowerShell lets you call it in the same way that you call other programs and existing tools. Running a script does the same thing as running all the commands in that script.

PowerShell introduces a few features related to running scripts and tools that may at first confuse you if you aren’t aware of them. For more information about how to call scripts and existing tools.

The first time you try to run a script in PowerShell, PowerShell provides the error message:

File c:\tools\myFirstScript.ps1 cannot be loaded because the execution of scri pts is disabled on this system. Please see "gethelp about_signing" for more d etails. At line:1 char:12

+ myFirstScript <<<<

Since relatively few computer users write scripts, PowerShell’s default security policies prevent scripts from running. Once you begin writing scripts, though, you should configure this policy to something less restrictive.

When it comes to the filename of your script, picking a descriptive name is the best way to guarantee that you will always remember what that script does—or at least have a good idea. This is an issue that PowerShell tackles elegantly, by naming every cmdlet in the VerbNoun pattern: a command that performs an action (verb)onan item (noun). As an example of the usefulness of this philosophy, consider the names of typical Windows commands given in Example 101:

Example 101. The names of some standard Windows commands

PS >dir $env:WINDIR\System32\*.exe | SelectObject Name

Name

accwiz.exe actmovie.exe ahui.exe alg.exe append.exe arp.exe asr_fmt.exe asr_ldm.exe asr_pfu.exe at.exe atmadm.exe attrib.exe (...)

Compare this to the names of some standard Windows PowerShell cmdlets given in Example 102.

Example 102. The names of some standard Windows PowerShell cmdlets

PS >GetCommand | SelectObject Name

Name

AddContent AddHistory AddMember AddPSSnapin ClearContent ClearItem ClearItemProperty ClearVariable CompareObject ConvertFromSecureString ConvertPath ConvertToHtml (...)

As an additional way to improve discovery, PowerShell takes this even further with the philosophy (and explicit goal) that “you can manage 80 percent of your system with less than 50 verbs.As you learn the standard verbs for a concept (such as Get as the standard verb of Read, Open, and so on), you can often guess the verb of a command as the first step in discovering it.

When you name your script (especially if you intend to share it), make every effort to pick a name that follows these conventions.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say