Windows 7 / Getting Started

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

  1. Launch the Windows PowerShell ISE.
  2. 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.
  3. 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.
  4. 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.
  5. Move your cursor to the bottom of the screen, type C:\Scripts\LogInfo.ps1, and press Enter. The script will execute.
  6. Change the current location to the script file with this command:
    Set-Location C:\Scripts.
  7. 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.
  8. 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

  1. Click File → New to create a blank area to work with a new script.
  2. 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).
  3. 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
  4. 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"}
[Previous] [Contents] [Next]

In this tutorial:

  1. Working with the Command Prompt
  2. Starting and Ending a Command Prompt Session
  3. Easy ways to invoke administrator Command Prompt sessions
  4. Starting Command Prompt at a Particular Folder
  5. Strings with Spaces Need Quotes
  6. Cmd.exe vs. Command.com
  7. Commands Are Not Case Sensitive
  8. Starting Command Prompt and Running a Command
  9. Cmd.exe and Other Command Prompts
  10. Using AutoRun to Execute Commands When Command Prompt Starts
  11. Using Cmds Command-Line Syntax
  12. Using Commands
  13. Type /? for help
  14. Starting Programs
  15. Open Windows Explorer at the current Command Prompt folder
  16. Using File-Name and Folder-Name Completion
  17. Use a different completion character
  18. Using Wildcards
  19. Editing the Command Line
  20. Using Command Symbols
  21. The Redirection Symbols
  22. The Pipe Symbol
  23. The Command Combination Symbols
  24. Pausing or Canceling Commands
  25. Simplifying Command Entry with Doskey Macros
  26. DOSKEY Saves Typing
  27. System Variables Identify the Environment
  28. Viewing Environment Variables
  29. Modifying Environment Variables
  30. Predefined Environment Variables
  31. Customizing Command Prompt Windows
  32. Setting the Window Size and Position
  33. Setting the Window Size and Position Visually
  34. Selecting a Font
  35. Setting Colors
  36. Setting Other Options
  37. Copy and paste in the command prompt window
  38. Navigating from the command prompt
  39. Printing a list of filenames
  40. Commands Use Paths
  41. Identifying Executables
  42. Modifying the Path to Executables
  43. Modifying the Path with the GUI
  44. Changing the Current Path with CD
  45. Changing the Current Path with Windows Explorer
  46. Capturing the Output
  47. A Sampling of Commands
  48. Dir
  49. Copy
  50. XCopy
  51. SET
  52. NET USE
  53. SystemInfo
  54. DriverQuery
  55. Echo
  56. Advanced Shell Commands
  57. Creating a Batch File
  58. Scheduling a Batch File
  59. Creating Scheduled Tasks with a Script
  60. Using Windows PowerShell and the PowerShell ISE
  61. Windows PowerShell ISE
  62. PowerShell Commands
  63. Verbs and Nouns
  64. Sending Output to a Text File
  65. PowerShell Syntax
  66. Variables Created with a $ Symbol
  67. Comparison Operators
  68. Parentheses, Brackets, and Braces
  69. Running PowerShell Scripts
  70. PowerShell Execution Policy
  71. Changing the Execution Policy
  72. Looping
  73. Collections
  74. Creating a PowerShell Script
  75. Documenting Scripts
  76. Using PowerShell Commands
  77. Getting Help on PowerShell
  78. Using WMI_Cmdlets
  79. Getting Details on an Object
  80. Querying Information on Specific Objects
  81. Terminate Applications with Win32_process
  82. Formatting Output with the -f Format Operator
  83. Filtering the Output with the Where-Object Command
  84. Using the IF statement
  85. Using the Switch Statement
  86. Script Reusability