PowerShell Basics: Installation, Top 10 Key Commands, and Common Code

PowerShell Basics: Installation, Top 10 Key Commands, and Common Code
Photo by NEOM / Unsplash

PowerShell is a command-line shell and scripting language designed for system administrators and power users. It provides a powerful and flexible tool for automating tasks and managing systems.

PowerShell is built on top of the .NET framework and is available for Windows, Linux, and macOS. It allows users to interact with various system components, such as the file system, registry, and network, through a set of commands called cmdlets.

One of the key benefits of using PowerShell is its ability to automate repetitive tasks. With PowerShell, you can write scripts to perform complex operations, such as deploying software, managing user accounts, and configuring network settings. These scripts can be executed locally or remotely, making it easy to manage systems across multiple machines.

Additionally, PowerShell supports object-oriented programming, allowing you to work with structured data and perform operations on objects. This makes it easier to manipulate and transform data, especially when dealing with large datasets.

Overall, PowerShell is a versatile and powerful tool that can greatly enhance productivity for system administrators and power users. Whether you're managing a single machine or a large network of systems, PowerShell provides the tools you need to streamline your workflow and automate routine tasks.

Why Should You Learn PowerShell?

There are several reasons why learning PowerShell can be beneficial:

  1. Automation: PowerShell allows you to automate repetitive tasks, saving you time and effort. By writing scripts, you can automate complex operations and streamline your workflow.
  2. System Management: PowerShell provides powerful tools for managing systems, including file systems, registry settings, and network configurations. With PowerShell, you can efficiently manage and maintain your systems.
  3. Scripting: PowerShell is a scripting language that supports object-oriented programming. It allows you to work with structured data and perform operations on objects, making it easier to manipulate and transform data.
  4. Cross-Platform Compatibility: PowerShell is available not only on Windows but also on Linux and macOS. This cross-platform compatibility allows you to use PowerShell across different operating systems.
  5. Integration with Other Technologies: PowerShell integrates well with other Microsoft technologies, such as Active Directory, Exchange Server, and Azure. This makes it a valuable tool for managing and administering these technologies.
  6. Community and Resources: PowerShell has a large and active community of users and developers. There are plenty of online resources, forums, and tutorials available to help you learn and troubleshoot PowerShell.

By learning PowerShell, you gain a versatile and powerful tool that can enhance your productivity as a system administrator or power user. Whether you need to automate tasks, manage systems, or work with data, PowerShell provides the capabilities you need.

How to install PowerShell?

1- Windows

PowerShell is pre-installed on Windows 10 and Windows Server 2016 and later versions. You can access it by searching for "PowerShell" in the Start menu, or by opening the Command Prompt and typing powershell.

2- Linux

On Linux, you can install PowerShell by following the instructions specific to your distribution. For example, on Ubuntu, you can run the following commands in the terminal:

wget -q <https://packages.microsoft.com/config/ubuntu/$>(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell

Once installed, you can start PowerShell by running the pwsh command in the terminal.

3- macOS

On macOS, you can install PowerShell using Homebrew. Open the Terminal and run the following commands:

brew update
brew install --cask powershell

After the installation, you can start PowerShell by running the pwsh command in the Terminal.

To verify that PowerShell is installed correctly, open your preferred terminal and run the pwsh command. You should see the PowerShell prompt if the installation was successful.

Now you have PowerShell installed and can start utilizing its power and flexibility for various tasks and automation.

10 Top Common Commands for PowerShell

Here are some of the top commands for PowerShell:

  1. Get-Process: This command retrieves information about the processes running on the system.
  2. Get-Service: It retrieves the status and other information about services running on the system.
  3. Get-EventLog: This command allows you to access and manage event logs on the system.
  4. Get-ChildItem: It lists the files and directories in a specified location.
  5. Set-ExecutionPolicy: This command is used to configure the execution policy for PowerShell scripts.
  6. New-Item: It creates a new item, such as a file or directory.
  7. Remove-Item: This command is used to delete items, such as files or directories.
  8. Copy-Item: It copies files or directories from one location to another.
  9. Invoke-WebRequest: This command retrieves content from a web page or web service.
  10. Start-Process: It starts a new process.

10 Tricks and Code Snippets for PowerShell

1- Looping through an Array

$myArray = @(1, 2, 3, 4, 5)
foreach ($item in $myArray) {
    Write-Host $item
}

2- Checking if a File Exists

$file = "C:\\path\\to\\file.txt"
if (Test-Path $file) {
    Write-Host "File exists"
} else {
    Write-Host "File does not exist"
}

3- Getting the Current Date and Time

$dateTime = Get-Date
Write-Host $dateTime

4- Creating a New Directory

$directory = "C:\\path\\to\\new\\directory"
New-Item -ItemType Directory -Path $directory

5- Reading and Writing to a CSV File

$data = Import-Csv -Path "C:\\path\\to\\file.csv"
$data | Export-Csv -Path "C:\\path\\to\\newfile.csv" -NoTypeInformation

6- Running a Command as Administrator

Start-Process -FilePath "C:\\path\\to\\executable.exe" -Verb RunAs

7- Sorting an Array

$myArray = @(5, 3, 1, 4, 2)
$sortedArray = $myArray | Sort-Object
Write-Host $sortedArray

8- Getting the Last Modified Date of a File

$file = "C:\\path\\to\\file.txt"
$lastModified = (Get-Item $file).LastWriteTime
Write-Host $lastModified

9- Working with Environment Variables

$env:PATH += ";C:\\new\\path"
$env:PATH

10- Checking if a Service is Running

$serviceName = "MyService"
$serviceStatus = Get-Service -Name $serviceName
if ($serviceStatus.Status -eq "Running") {
    Write-Host "Service is running"
} else {
    Write-Host "Service is not running"
}

These are just a few examples of the many commands and tutorials available in PowerShell. You can explore more commands and their functionalities in the PowerShell documentation.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+