ps1 script to list running services

Services

Use PowerShell ISE to Create ps1 script

PowerShell Integrated Scripting Environment (ISE) is a powerful tool for IT professionals, developers, and administrators to automate tasks and manage systems. In this article, we will explore how to use PowerShell ISE and create a PowerShell script (.ps1) to list all running services and their statuses. By the end, you’ll have a better understanding of PowerShell ISE and its capabilities, along with a functional script for system administration tasks.


What is PowerShell ISE?

PowerShell ISE (Integrated Scripting Environment) is a graphical host application for Windows PowerShell. It provides a user friendly interface for writing, testing, and debugging windows PowerShell scripts unlike command line (CMD). While Windows Terminal and Visual Studio Code are popular alternatives, PowerShell ISE remains a favorite for quick scripting and debugging due to its GUI’s simplicity.

Key Features of PowerShell ISE:

  1. Syntax Highlighting: Makes your code more readable by coloring commands, parameters, and strings.
  2. Code Completion: Helps you write commands faster with IntelliSense-like functionality.
  3. Integrated Debugging: Allows you to set breakpoints and debug scripts directly within the interface.
  4. Tabbed Interface: Enables working on multiple scripts simultaneously.

How to Access PowerShell ISE

To open PowerShell ISE:

  1. Press Win + S, type “PowerShell ISE,” and select it from the search results.
  2. Alternatively, run powershell_ise from the Run dialog (Win + R).
  3. In some cases, you may need administrative privileges to run specific commands. Right-click PowerShell ISE and select Run as Administrator.

Writing a PowerShell Script to List Running Services and Their Status

One of the most common tasks for administrators is managing and monitoring services. The script we’ll create lists all running services and displays their status.


Steps to Create the Script

  1. Open PowerShell ISE:
  • Launch PowerShell ISE and create a new script by clicking on File > New or pressing Ctrl + N.
  1. Write the Script:
  • Use the following code snippet:
# Script to list all running services and their status

# Define output format
Write-Host "Service Name and Status" -ForegroundColor Cyan
Write-Host "=========================" -ForegroundColor Cyan

# Get services and filter by status
Get-Service | ForEach-Object {
    $serviceName = $_.DisplayName
    $serviceStatus = $_.Status
    Write-Host "$serviceName : $serviceStatus" -ForegroundColor Green
}

# End of script
Write-Host "\nScript Execution Completed." -ForegroundColor Yellow
  1. Save the Script:
  • Save the file with a .ps1 extension. For example, name it ListRunningServices.ps1.
  1. Run the Script:
  • To execute the script, press F5 in PowerShell ISE, or run it from the console using:
..\uListRunningServices.ps1

Understanding the Script

  • Get-Service: Retrieves all services on the local system.
  • ForEach-Object: Iterates through each service to process its properties.
  • Write-Host: Displays output to the console with optional text color for readability.
  • DisplayName and Status: Properties of the Get-Service output used to display the service name and its status.

Adding Advanced Features

You can enhance the script to ask for the user input to request for any of either running or stopped services. Here’s an advanced version:

# Script to list services based on user input (running, stopped, or all)

# Prompt the user for input
Write-Host "Select the status of services to list:" -ForegroundColor Cyan
Write-Host "1. Running" -ForegroundColor Green
Write-Host "2. Stopped" -ForegroundColor Red
Write-Host "3. All Services" -ForegroundColor Yellow
$choice = Read-Host "Enter your choice (1, 2, or 3)"

# Validate user input
if ($choice -notin @("1", "2", "3")) {
    Write-Host "Invalid choice. Please run the script again and enter 1, 2, or 3." -ForegroundColor Red
    exit
}

# Filter services based on user choice
switch ($choice) {
    "1" { $services = Get-Service | Where-Object { $_.Status -eq "Running" } }
    "2" { $services = Get-Service | Where-Object { $_.Status -eq "Stopped" } }
    "3" { $services = Get-Service }
}

# Display the output in a formatted table
if ($services) {
    $services | Select-Object DisplayName, Status | Format-Table -AutoSize -Wrap
} else {
    Write-Host "No services found with the selected status." -ForegroundColor Yellow
}

# End of script
Write-Host "End of Script. Execution is now Completed." -ForegroundColor Yellow

To run this version, follow the same steps as above. The script filters only current state of services on the machine its being executed on.


Debugging in PowerShell ISE

  1. Set Breakpoints: Click the line number where you want execution to pause and press F9.
  2. Step Through Code: Use F10 to step through each line of the script.
  3. Inspect Variables: Hover over variables or type their names in the Console pane to see their values.

Advantages of Using PowerShell ISE

  1. Ease of Use: Intuitive interface for beginners and advanced users.
  2. Integrated Console: Run commands and debug scripts without leaving the environment.
  3. Customization: Change themes, fonts, and other settings to suit your preferences.

Conclusion

PowerShell ISE is a versatile and user-friendly environment for writing, testing, and debugging scripts. The example script provided here demonstrates how to manage services effectively using PowerShell. By following the steps outlined, you can enhance your administrative tasks and automate routine processes.

If you’re just getting started with PowerShell, ISE is an excellent place to hone your skills before moving to more advanced tools like Visual Studio Code.