PowerShell is a command-line scripting language developed by Microsoft that is designed to automate administrative tasks and manage system configurations. It provides a powerful framework for managing Windows-based systems and automating repetitive tasks.
Scripts are written in the PowerShell scripting language, which is based on the .NET framework.
The language includes features such as variables, loops, conditionals, and functions, which allow you to write scripts to perform a wide range of tasks.
In this guide, I’ll guide you through the process of getting started with PowerShell, and provide some practical examples of how you can use it to perform some common tasks.
Getting Started
Click on the Start
menu and type “PowerShell” in the search bar.
or
Press Win
+X
, you may see underlines under the options. Press I
, PowerShell will open.
Win
+X
A
for an elevated prompt
Once you have opened the PowerShell console, you can start entering commands.
PowerShell commands are called cmdlets
, and they are structured in a verb-noun format.
Check PS Version
To verify which version of PowerShell you are running, run:
|
|
List Running Processes
For example, to display a list of all the processes running on your machine, you would enter the following command:
|
|
This will display a list of all the running processes, including their names, process IDs, and memory usage.
Execution Policy
The execution policy is designed to prevent a user from unknowingly running a script. Although not a security boundary, it prevents accidental launching of scripts.
|
|
PowerShell scripts can’t be run at all when the execution policy is set to Restricted
. This is the default setting on all Windows client operating systems.
If you need to set the execution policy, run Set-ExecutionPolicy
with the recommended RemoteSigned
policy.
|
|
Quick Practical Examples
Checking Disk Space
|
|
Restarting a Service
For example, to restart the Windows Update service, you would enter the following command:
|
|
Creating a User Account
The following command creates a new user account named John
with the password P@ssw0rd
:
|
|
Removing Old Files
The following command removes all files in the C:\Temp
directory that are older than 30 days:
|
|
Moving Files by File Extension
This is a PowerShell script that moves files of a given file extension from a source directory to a destination directory on the OS all while promting for user input.
|
|
Conclusion
PowerShell is a valuable tool for system administrators, enabling them to automate tasks and streamline their Windows environment.
With its powerful capabilities and user-friendly interface, it is an essential tool for any Windows-based IT environment.
Learn More
- Windows PowerShell Survival Guide
- MS introduction to PowerShell
- Official PowerShell Documentation
- PowerShell Overview
- Some good examples