Home » Tech Tips » Windows » Beginner’s Guide to Windows PowerShell

Beginner’s Guide to Windows PowerShell

Windows PowerShell is a powerful command-line interface and scripting environment developed by Microsoft. It is designed to help Windows computer users to automate repetitive tasks, configure system settings, and manage Windows systems with precision and efficiency. With its combination of a robust scripting language and an interactive shell, PowerShell has become a fundamental tool in Windows OS landscape.

Why to Use PowerShell?

Since Windows also comes with the most popular Command Prompt app, many users hesitate to use PowerShell. You can refer our earlier article for the complete comparison of Command Prompt and Windows PowerShell apps. Here are some reasons why PowerShell is useful:

  • PowerShell enables users to automate tasks that would otherwise be tedious and time-consuming if performed manually.
  • With a single command or script, you can accomplish tasks across multiple computers or users.
  • PowerShell can interact with many parts of Windows, third-party applications, and even cloud services.
  • Scripts and commands are reusable and adaptable, ensuring consistent results across environments.

Getting Started: Opening PowerShell

PowerShell comes pre-installed on Windows computers and here’s how you can launch it:

  • Type powershell into the Start Menu or Windows search box to find and open the app. For administrator privileges, select “Run as Administrator” option from the search result’s right pane.
Open PowerShell App in Windows 11
Open PowerShell App in Windows 11
  • Press “Windows Logo + X” keys or right-click on the Start button to open the power menu. Select “Terminal (Admin) option to the command-line interface. If Terminal app opens with Command Prompt, press “Control + Shift + 1” or choose “Windows PowerShell” from the title bar new tab arrow.
Open Terminal Admin App
Open Terminal Admin App

Once open, you will see a blue or black window with a prompt like PS C:\ >. Now, you are ready to start entering commands!

Understanding the PowerShell Interface

The PowerShell interface, or “console”, is where you’ll write and execute commands. Let’s break down some key elements:

  • Prompt: The line that shows your current working directory and waits for your input.
  • Cmdlets: Pronounced “command-lets,” these are built-in PowerShell commands that perform specific functions.
  • Parameters: Options (also called switches) you provide to cmdlets to specify their behavior.

Basic Commands Every Beginner Should Know

Here are some basic PowerShell commands, simply type them and press enter key to get the results.

  • Get-Help: The most important cmdlet for beginners. Type Get-Help followed by any command to display help information. For example: Get-Help Get-Process will show the help for Get-Process command.
Get-Help in PowerShell
Get-Help in PowerShell
  • Get-Command: Lists all available commands in PowerShell.
  • Get-Process: Shows running processes on your computer.
  • Get-ChildItem: Lists files and folders in a directory.
  • Clear-Host: Clears the console screen (CLS command also works).
  • Set-Location: Changes your current directory (like CD in Command Prompt, CD also works in PowerShell). For example: Set-Location C:\Windows to change the directory.
Set Location for Prompt
Set Location for Prompt

PowerShell cmdlets follow a consistent “Verb-Noun” naming convention, which makes them easy to guess and remember. For example:

  • Get-Item retrieves items (files, folders, registry keys, etc.).
  • Set-Item changes the value of an item.
  • Remove-Item deletes an item.

This pattern helps you explore and discover new cmdlets as you become more comfortable with PowerShell.

Working with Files and Folders

Managing files and directories is a common task. Here are some practical cmdlets:

CommandDescriptionExample
New-ItemCreates a new file or folderNew-Item -Path . -Name "Example.txt" -ItemType "File"
Copy-ItemCopies files or foldersCopy-Item -Path "Example.txt" -Destination "Backup\Example.txt"
Move-ItemMoves files or foldersMove-Item -Path "Example.txt" -Destination "Documents\"
Remove-ItemDeletes files or foldersRemove-Item -Path "OldFile.txt"

Pipelining: Combining Commands

One of PowerShell’s most powerful features is its pipeline, which allows you to chain commands together using the | (pipe) symbol. This lets the output of one cmdlet become the input for another. Let’s take an example of the below command:

Get-Process | Sort-Object CPU -Descending | Select-Object -First 5

This command lists the top five processes consuming the most CPU on your system.

Variables and Scripts

Variables in PowerShell are easy to use and always begin with a $ symbol. For example:

$name = "World"
Write-Output "Hello, $name!"
Run Script in PowerShell
Run Script in PowerShell

Though the above script works with two separate lines, generally, scripts are simple text files containing a collection of commands and logic, saved with a .ps1 extension. You can run scripts by typing their path:

.\myscript.ps1

Before running scripts, you may need to set the execution policy:

Set-ExecutionPolicy RemoteSigned

You should run this as command in administrator mode.

Quick Tips for Executing Command

  • You can also open multiple tabs in Terminal window to work on parallel sessions.
  • Some of the basic Windows or DOS commands like CD, CLS, and DIR also work in PowerShell. So, you can make use of them but not all the commands will work.
  • You can simply copy the command to clipboard from anywhere and paste it in the prompt.
  • Use up and down arrows to navigate through previous command history.
  • Commands are not case sensitive though syntax highlighting is used to shows it in an easy-to-read format.

Customizing Your PowerShell Experience

You can personalize the PowerShell window to suit your style. Click the arrow on the Terminal title bar and select “Settings” or press “Control + Comma” shortcut keys to open app’s settings page.

Open Terminal Settings
Open Terminal Settings
  • Go to “Startup” tab and set the “Default profile” as “Windows PowerShell. This will help to open PowerShell whenever you click the + icon on title bar (a new tab) and whenever you launch Terminal app.
  • Navigate through other sections to change colors, fonts, and window size (most options are available under “Profiles > Windows PowerShell > Appearance” section).
  • You can also default the profile to (primarily under “Profile > Defaults” section) to setup the starting directory, running with administrator option, automatically load your favorite functions, aliases, and shortcuts every time you start PowerShell.

Check our guide on using Terminal app for complete details.

Advanced Topics in PowerShell

Once you’re comfortable, you can explore more advanced features:

  • Modules: Collections of cmdlets and functions you can import to extend PowerShell’s capability (e.g., Import-Module Azure).
  • Remoting: Manage other computers using PowerShell over the network.
  • Desired State Configuration (DSC): Define and automate your system’s configuration.
  • Integrating with APIs and Cloud: Use PowerShell to interact with REST APIs, cloud platforms like Azure, and more.

Conclusion

Windows PowerShell is more than just a command-line tool – it’s a gateway to automating, managing, and mastering your Windows environment. With a bit of exploration, practice, and curiosity, you’ll discover that PowerShell can transform the way you interact with your computer. If you encounter errors or need guidance, the PowerShell community is vast and supportive. You can make use of the Microsoft Learn tutorials and get advice from TechNet or Stack Overflow forums.

Leave a Comment

Your email address will not be published. Required fields are marked *