10 PowerShell Examples You Need to Know

by | PowerShell

If you’ve ever dipped your toes into the world of system administration, or even just messed around with some heavy-duty computer tasks, you’ve probably heard of this nifty little tool called PowerShell. It’s not just a flashy name, by the way—it really does pack some serious ‘power’!

PowerShell is a powerful Windows administration, automation, and management scripting language. The 10 PowerShell examples listed in this article will help you simplify everyday tasks and optimize processes. From navigating directories, manipulating files, and interactively exploring system values, these examples will provide you with the foundation needed to take full advantage of its capabilities.

10 PowerShell Examples You Need to Know

Mastering the essentials of PowerShell can make you more efficient as a system administrator. By understanding and honing your skills with these 10 essential PowerShell commands, you’ll become more proficient in using this valuable resource.

Let’s get into it!

How to Get Started With PowerShell

Before we start with the examples, let’s quickly review how you can get started with running commands in the PowerShell command line.

To run PowerShell commands, open the PowerShell console by searching for “PowerShell” in the Windows Start menu. When you start PowerShell, you’ll be presented with the PowerShell prompt. This is designed to facilitate the execution of PowerShell commands, or “cmdlets.”

Some basic cmdlets you should know include:

  1. Get-Help: The get-help cmdlet provides online help for any other PowerShell command. Use this to learn about a cmdlet’s syntax and parameters.
  2. Get-Command: Retrieves a list of all basic PowerShell cmdlets whose modules have been loaded. This can help you find the appropriate cmdlet for your needs.

What is PowerShell Scripting?

Scripts in PowerShell environment can help streamline repetitive tasks and organize your cmdlets. PowerShell scripts are typically saved with a .ps1 file extension.

What is PowerShell Scripting

The following are some key components when using a scripting language:

  • Variables: Using variables to store values for use or modification in your Windows PowerShell scripts. In PowerShell, variables are defined using the dollar sign ($) followed by the variable name (e.g., $varName = “value”).
  • Control structures: PowerShell supports various control structures, such as if statements, for loops, and while loops, allowing you to construct more complex scripts.
  • Functions: You can create custom functions in your scripts to reuse code. Functions are defined using the function keyword followed by the function name and a script block enclosed in curly braces ({}).

Why Use PowerShell?

PowerShell’s main purpose is to automate administrative tasks for Microsoft Windows systems.

The following are some vital PowerShell commands for managing and automating administrative tasks:

  • Get-Process: Displays a list of all the running processes on your computer.
  • Get-Service Command: Retrieves the status of services on your computer.
  • Set-ExecutionPolicy: Sets the default execution policy for Windows PowerShell scripts, allowing you to control their security level.
  • Install-Module or Install-Script: Installs modules or scripts from a repository, such as the PowerShell Gallery.
  • Remove-Item: Deletes files or folders on your system.

10 PowerShell Examples

Now that we’ve reviewed the basics of PowerShell commands, let’s get into the 10 essential and basic PowerShell commands every system admin should know.

10 Essential PowerShell Examples

All the commands have examples included to help you better understand them.

1. Listing All Files in a Directory

When you need to view all files in a particular directory, you can use the Get-ChildItem command.

The command syntax for listing all files in a directory is:

Get-ChildItem -Path C:\Your\Path\Here

Let’s say I want to list all the files in my local drive E. Running the above command will give the following output:

Listing all files in a directory in command prompt

2. Creating a New Directory

When you want to create a new directory (folder) using Windows PowerShell, you can use the New-Item command.

The following command creates a new folder named “NewFolder”:

Creating a New Directory in command prompt

3. Creating a New File

To create a new file such as a text file, csv file, or html file, you can use the New-Item command in your current PowerShell session, just like we did with creating a new directory.

The following command shows how you use the command prompt to create a new file named NewFile.txt

New-Item -Path C:\Your\Path\Here -Name "NewFile.txt" -ItemType "file"

This command creates a new file named “NewFile.txt” at the specified path.

Creating a New File in command prompt

4. Renaming a File

If you want to rename a file, you can use the Rename-Item command.

The following command shows how to rename a file from “OldName.txt” to “NewName.txt”:

Rename-Item -Path C:\Your\Path\Here\OldName.txt -NewName NewName.txt

This command renames the file “OldName.txt” to “NewName.txt” at the specified path.

5. Deleting a File

When you need to delete a file, you can use the Remove-Item command.

The following PowerShell script shows deleting a file named “FileName.txt”:

Remove-Item -Path C:\Your\Path\Here\FileName.txt

This command deletes the file “FileName.txt” at the specified path from the command line.

6. Reading The Content of a File

To read the content of a file, you can use the Get-Content command.

The following command reads the contents of a file named “FileName.txt”:

Get-Content -Path C:\Your\Path\Here\FileName.txt

This command displays the content of the file “FileName.txt” at the specified path.

Reading the content of a file in command prompt

7. Getting the Process Details

When you need information about the running processes on your system, including Windows PowerShell background jobs, you can use the Get-Process command.

The following PowerShell command is an example of getting the process details from the command line:

Get-Process

The get-process cmdlet lists all the processes currently running on your system, including their Process ID (PID), CPU usage, memory usage, and more.

Getting the process details in command prompt

8. Stopping a Process

If you need to stop a running process, you can use the Stop-Process command.

The following PowerShell command shows how to stop a process with a specific Process ID (PID) from the command line:

Stop-Process -Id PID

This command stops the process with the specified Process ID (PID). Remember to replace PID with the actual Process ID of the process you want to stop.

9. Getting System Information

To retrieve detailed information about your system, you can use the Get-ComputerInfo command.

The following PowerShell command prompt retrieves system information from the command line:

Get-ComputerInfo

This command provides a comprehensive report on your system, including operating system details, hardware specifications, and more.

Getting system information in command prompt

10. Running a Command on a Remote Computer

If you need to run a command on a remote computer, you can use the Invoke-Command command.

This example shows how to retrieve the list of running processes on a remote computer:

```powershell
Invoke-Command -ComputerName "RemotePC" -ScriptBlock { Get-Process }
```

This command runs the Get-Process command on a remote computer named “RemotePC”. You’ll need to replace “RemotePC” with the name of the remote computer. You can use this technique to run scripts created remotely.

The above Powershell commands are some of the most important commands that you should know when automating tasks. After understanding the basic PowerShell scripts, you can leap toward more advanced topics.

To learn more about PowerShell commands, check the following video out:

Final Thoughts

We’ve explored 10 PowerShell commands, from the basics to the more advanced, but there’s a whole universe out there for you to discover. Every command you’ve learned today is a tool in your tech toolkit, ready to help you whip your system into shape with just a few keystrokes.

Mastering PowerShell can be a game changer because with it, you have the key to automating tasks, managing systems, and so much more. You can easily list files, create directories, and even control processes.

The beauty of PowerShell lies in its versatility and depth. As you dive deeper, you’ll find that it can streamline your workflow, making tasks that once took hours just a matter of a few commands. Moreover, it provides you with the skills to manage your local computer and remote systems.

Now, go forth and script!

Frequently Asked Questions

Frequently Asked Questions

What are the essential PowerShell commands for system administrators?

System administrators need to be familiar with a variety of PowerShell commands to effectively manage their systems.

Some essential commands include Get-Service, Get-Process, Get-EventLog, and Stop-Process. These commands help in managing services, processes, and event logs efficiently.

How can I use PowerShell for automation tasks?

PowerShell is an automation tool designed for system administrators and IT professionals.

With PowerShell, you can automate repetitive tasks by writing scripts to execute a sequence of cmdlets, control structures, and other functions.

Some useful cmdlets for automation include Invoke-WebRequest, Import-Csv, and New-ScheduledTask.

Which PowerShell cmdlets are most useful for IT professionals?

The most useful PowerShell cmdlets for IT professionals largely depend on their specific needs and tasks. However, some useful cmdlets include Get-ADUser, Get-ADComputer, and Test-Connection, allowing IT professionals to query user and computer information from Active Directory and test network connections, respectively.

What are some cool PowerShell script examples to enhance desktop support?

PowerShell can be used to create scripts that automate tasks and improve desktop support. For example, you can create a script to:

  1. Retrieve system information including OS version, hardware, and uptime.
  2. Uninstall applications on multiple computers.
  3. Monitor and manage Windows services, processes, and event logs.
  4. Quickly perform bulk user and group management in Active Directory.

Where can I find a comprehensive list of PowerShell commands and their usage?

A comprehensive list of PowerShell commands, also known as cmdlets, can be found in the official Microsoft documentation. You can also use the Get-Command cmdlet within PowerShell to retrieve a list of available cmdlets for your installed version.

author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts