Creating a PowerShell Script
With the basics out of the way, it's time to walk through the creation of some basic scripts using the Windows PowerShell Integrated Scripting environment. Modified the Execution Policy so that scripts can be run.
Creating and Running a PowerShell Script
- Launch the Windows PowerShell ISE.
- Click in the top pane (labeled Untitled1.ps1), type in the following text, and press Enter:
Get-EventLog -List
Notice the command doesn't execute when you press Enter but instead starts a line 2. - Click the green button to run the script. You can also press F5. You'll see the result
displayed in the middle pane. If there is a syntax error, you'll see a red text error
instead of black text as the output.
If you do see a red error message, look for these characters: <<<<. They will often point to the specific string of text that is causing the problem. - Click the Save icon on the menu (or press Ctrl+S). Browse to a folder named C:\ Scripts (create it if you need to), and change Untitled to LogInfo to save the script as C:\Scripts\LogInfo.ps1.
- Move your cursor to the bottom of the screen, type C:\Scripts\LogInfo.ps1, and press Enter. The script will execute.
- Change the current location to the script file with this command:
Set-Location C:\Scripts.
- Try to run the script with this command (it will fail):
LogInfo.ps1
Even though the script is in the current path, PowerShell needs a hint to find it. - Press the up arrow, and modify the last command so it looks like this:
.\LogInfo.ps1
The .\ characters tell PowerShell the script is in the current path.
Modifying a Script Using a ForEach Loop
- Click File → New to create a blank area to work with a new script.
- Enter the following text, and press F5 to execute it.
get-service | select-object name, status
This lists all the services on the system by name and their current status (such as running or stopped). - Instead of outputting this data to the screen, you can store it in a collection. The following
command will store the result in a collection named $colService. Notice that
you don't have to retype the entire command, but instead you just add $colService =
to the beginning of the line:
$colService = Get-service | select-object name, status
- Now add a ForEach loop to loop through the collection. The loop will check each service
stored in the collection and then use the Write-Host cmdlet to display only the
services with a status equal to "running".
ForEach ($item in $colService) { If ($item.status -eq "running") {write-host $item.name} }
Similarly, you could use the same method to list all the services that are stopped by simply changing "running" to "stopped" in the script.
While this example does show how a ForEach loop can be used to loop through a collection, it's not the only way to get a list of running services. As with most scripts, there's more than one way to get what you need. For example, the following one-liner could also be used to get a list of running services:
Get-Service | Select name, status | where {$_.status -eq "running"}
In this tutorial:
- Working with the Command Prompt
- Starting and Ending a Command Prompt Session
- Easy ways to invoke administrator Command Prompt sessions
- Starting Command Prompt at a Particular Folder
- Strings with Spaces Need Quotes
- Cmd.exe vs. Command.com
- Commands Are Not Case Sensitive
- Starting Command Prompt and Running a Command
- Cmd.exe and Other Command Prompts
- Using AutoRun to Execute Commands When Command Prompt Starts
- Using Cmds Command-Line Syntax
- Using Commands
- Type /? for help
- Starting Programs
- Open Windows Explorer at the current Command Prompt folder
- Using File-Name and Folder-Name Completion
- Use a different completion character
- Using Wildcards
- Editing the Command Line
- Using Command Symbols
- The Redirection Symbols
- The Pipe Symbol
- The Command Combination Symbols
- Pausing or Canceling Commands
- Simplifying Command Entry with Doskey Macros
- DOSKEY Saves Typing
- System Variables Identify the Environment
- Viewing Environment Variables
- Modifying Environment Variables
- Predefined Environment Variables
- Customizing Command Prompt Windows
- Setting the Window Size and Position
- Setting the Window Size and Position Visually
- Selecting a Font
- Setting Colors
- Setting Other Options
- Copy and paste in the command prompt window
- Navigating from the command prompt
- Printing a list of filenames
- Commands Use Paths
- Identifying Executables
- Modifying the Path to Executables
- Modifying the Path with the GUI
- Changing the Current Path with CD
- Changing the Current Path with Windows Explorer
- Capturing the Output
- A Sampling of Commands
- Dir
- Copy
- XCopy
- SET
- NET USE
- SystemInfo
- DriverQuery
- Echo
- Advanced Shell Commands
- Creating a Batch File
- Scheduling a Batch File
- Creating Scheduled Tasks with a Script
- Using Windows PowerShell and the PowerShell ISE
- Windows PowerShell ISE
- PowerShell Commands
- Verbs and Nouns
- Sending Output to a Text File
- PowerShell Syntax
- Variables Created with a $ Symbol
- Comparison Operators
- Parentheses, Brackets, and Braces
- Running PowerShell Scripts
- PowerShell Execution Policy
- Changing the Execution Policy
- Looping
- Collections
- Creating a PowerShell Script
- Documenting Scripts
- Using PowerShell Commands
- Getting Help on PowerShell
- Using WMI_Cmdlets
- Getting Details on an Object
- Querying Information on Specific Objects
- Terminate Applications with Win32_process
- Formatting Output with the -f Format Operator
- Filtering the Output with the Where-Object Command
- Using the IF statement
- Using the Switch Statement
- Script Reusability