Chat
Search
Ithy Logo

Essential PowerShell Commands for System Administrators

Unlock the full potential of your Windows systems with these powerful PowerShell commands.

powershell script execution

Key Takeaways

  • Comprehensive System Management: PowerShell offers commands that cover every aspect of system administration, from service management to network diagnostics.
  • Enhanced Automation Capabilities: Utilize scripts and commands to automate routine tasks, enhancing efficiency and reducing the potential for human error.
  • Detailed Monitoring and Reporting: Gain in-depth insights into system performance and health through specialized PowerShell commands.

Introduction to PowerShell Commands

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and the associated scripting language. It is built on the .NET framework and is designed to help system administrators automate tasks and manage system configurations more efficiently. The following list encompasses some of the most useful PowerShell commands that can significantly streamline your workflow.

Comprehensive PowerShell Command List

The table below provides a curated list of essential PowerShell commands, each accompanied by a detailed description, category classification, and relevant tags to help you navigate and utilize them effectively.

Name Command Description Category Hotkey Icon Path Use PowerShell Run as Admin Tags
List All Services
Get-Service
Retrieves a list of all services on the local computer. System Management None icons\services.png Yes No Services, System
Stop a Service
Stop-Service -Name 'ServiceName'
Stops a specific service by its name. System Management None icons\stop.png Yes Yes Services, System
Get Help on a Command
Get-Help -Name 'CommandName'
Provides detailed help information about a specific PowerShell command. Learning None icons\help.png Yes No Help, Documentation
List All Commands
Get-Command
Lists all available commands in PowerShell. Learning None icons\list.png Yes No Commands, List
Export Data to CSV
Get-Process | Export-Csv -Path 'C:\path\to\file.csv' -NoTypeInformation
Exports the output of a command to a CSV file. Data Management None icons\export.png Yes No Export, CSV, Data
List Directory Contents
Get-ChildItem -Path 'C:\path\to\directory'
Lists the contents of a specified directory. File Management None icons\folder.png Yes No Directory, Files
Get System Information
Get-ComputerInfo
Retrieves detailed information about the local computer. System Management None icons\system-info.png Yes No System, Information
Top 5 CPU Processes
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 -Property Name,Id,@{Name="Usage";Expression={ $uptime=New-TimeSpan -Start $_.StartTime; if($uptime.TotalSeconds -gt 0){[math]::Round( ($_.CPU/$uptime.TotalSeconds)*100,2 )} else {0} }},StartTime | Format-Table -AutoSize
Shows the top 5 CPU processes currently running. System Management None icons\adobe-photoshop-align horizontal centers.png Yes No CPU, Processor, Applications
Disk Space Overview
Get-PSDrive -PSProvider 'FileSystem' | Select-Object Name, @{Name='Used(GB)';Expression={[math]::Round(($_.Used/1GB),2)}}, @{Name='Free(GB)';Expression={[math]::Round(($_.Free/1GB),2)}}, @{Name='Total(GB)';Expression={[math]::Round(($_.Used+$_.Free)/1GB,2)}} | Format-Table -AutoSize
Shows an overview of disk space usage across all mounted file system drives. Storage Management None icons\disk-space.png Yes No Disk, Storage, FileSystem
Network Connection Stats
Get-NetTCPConnection | Group-Object -Property State | Select-Object Name,Count | Format-Table -AutoSize
Displays a count of TCP connections grouped by their state. Networking None icons\network.png Yes No Network, TCP, Connections
Recent Event Log Errors
Get-EventLog -LogName System -EntryType Error -Newest 10 | Format-Table TimeGenerated, EntryType, Source, Message -AutoSize
Fetches the 10 most recent error events from the System event log. Logging None icons\event-log.png Yes Yes EventLog, Error, System
List Running Services
Get-Service | Where-Object {$_.Status -eq 'Running'} | Select-Object Name, DisplayName, Status | Format-Table -AutoSize
Shows all currently running services on the system. System Management None icons\service-running.png Yes No Services, System, Diagnostics
System Information Overview
Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer, CsManufacturer, CsModel | Format-List
Retrieves comprehensive system and operating system properties. System Management None icons\computer-info.png Yes No System, Hardware, Information
Active Network Connections
Get-NetTCPConnection | Where-Object {$_.State -eq 'Established'} | Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, State | Format-Table -AutoSize
Lists all active TCP network connections. Network Management None icons\network-connections.png Yes No Network, Connections, TCP
Event Log Recent Errors
Get-WinEvent -LogName System -MaxEvents 20 | Where-Object {$_.LevelDisplayName -eq 'Error'} | Select-Object TimeCreated, Id, Message | Format-Table -Wrap
Retrieves the 20 most recent system error events. System Diagnostics None icons\error-log.png Yes No Logs, Errors, Diagnostics
Disk Space Usage
Get-PSDrive -PSProvider FileSystem | Select-Object Root, Used, Free, @{Name='PercentFree';Expression={[math]::Round(($_.Free/($_.Used+$_.Free))*100,2)}} | Format-Table -AutoSize
Shows disk space usage and percentage of free space. System Management None icons\hard-drive.png Yes No Disk, Storage, Space
Top 5 Memory Processes
Get-Process | Sort-Object WS -Descending | Select-Object -First 5 -Property Name,Id,WS,StartTime | Format-Table -AutoSize
Displays the top 5 processes consuming the most memory. System Management None icons\memory.png Yes No Memory, RAM, Applications
Restart DHCP Service
Restart-Service DHCP
Restarts the DHCP service. Network Management None icons\network.png Yes Yes DHCP, Network, Service
Get CPU Usage for a Process
Get-Counter '\\Process(cpu*)\\% Processor Time' | Select-Object -ExpandProperty CounterSamples | Select-Object InstanceName,@{Name="CPU %";Expression={[Decimal]::Round($_.CookedValue, 2)}}
Displays CPU usage for processes named 'cpu'. Performance Monitoring None icons\cpu-usage.png Yes No CPU, Performance
List All Files in a Directory Recursively
Get-ChildItem -Path C:\directory -Recurse
Lists all files and subdirectories within a specified directory. File Management None icons\folder.png Yes No Files, Directories
Stop a Process by Name
Stop-Process -Name notepad
Stops all instances of a process by its name. Process Management None icons\stop-process.png Yes No Process, Stop
Check Network Connection
Test-NetConnection -ComputerName google.com -Port 80
Tests network connectivity to a specific host and port. Network Management None icons\network-connection.png Yes No Network, Connectivity
Get System Information
Get-ComputerInfo
Displays detailed system and operating system properties. System Information None icons\system-info.png Yes No System, Info
List Available PowerShell Commands
Get-Command
Lists all available PowerShell commands. PowerShell Management None icons\powershell-commands.png Yes No PowerShell, Commands

Advanced PowerShell Usage

Beyond the basic commands, PowerShell offers advanced functionalities that can further enhance your system management capabilities. Scripts can be written to perform complex tasks, automate repetitive processes, and integrate with other tools and services.

Scripting and Automation

PowerShell scripts allow you to combine multiple commands and logic to perform intricate tasks. For example, automating the monitoring of system resources, deploying software across multiple machines, or managing user accounts can all be achieved through scripting.

# Example: Automate Service Restart
$services = "DHCP", "W32Time"
foreach ($service in $services) {
    Restart-Service -Name $service -Force
    Write-Output "$service has been restarted."
}

Integrating with Other Tools

PowerShell can seamlessly integrate with other Windows tools and third-party applications. This interoperability allows for comprehensive system management and the ability to extend PowerShell's capabilities through modules and add-ons.

Error Handling and Logging

Effective error handling ensures that your scripts can gracefully handle unexpected situations, while logging provides a record of actions taken and any issues encountered.

# Example: Error Handling in Scripts
Try {
    Stop-Service -Name "NonExistentService" -ErrorAction Stop
} Catch {
    Write-Error "Failed to stop service: $_"
}

Best Practices for PowerShell Usage

Adhering to best practices ensures that your use of PowerShell is both effective and secure.

Security Considerations

Always be cautious when running scripts from untrusted sources. Use the Get-Help command to understand what each script does before execution. Additionally, consider implementing execution policies and using digital signatures for your scripts.

Consistent Naming Conventions

Adopting consistent naming conventions for your scripts and functions makes them easier to understand and maintain. This practice is especially beneficial in collaborative environments.

Documentation and Comments

Well-documented scripts with clear comments enhance readability and facilitate easier troubleshooting and updates.

# Example: Documenting a Script
<#
.SYNOPSIS
    Restarts specified services.

.DESCRIPTION
    This script restarts a list of services provided in the $services array.

.PARAMETER None

.EXAMPLE
    ./Restart-Services.ps1
#>

$services = "DHCP", "W32Time"
foreach ($service in $services) {
    Restart-Service -Name $service -Force
    Write-Output "$service has been restarted."
}

Conclusion

PowerShell is an indispensable tool for system administrators, offering a vast array of commands that simplify and automate complex tasks. By mastering these essential commands and adhering to best practices, you can significantly enhance your efficiency and effectiveness in managing Windows environments.

References


Last updated February 9, 2025
Ask Ithy AI
Export Article
Delete Article