7 PowerShell Usage Examples

🚀 A cheat sheet style for quick information.


7 PowerShell Usage Examples


PowerShell is a task-based command-line and scripting language developed in .NET. Initially, it was just a Windows component.

It was written with C# and its source code is available on GitHub


Installation

PowerShell is already available for Windows 10 and 11, so you don’t need to install anything. But, if you are on another operating system, such as Ubuntu, for example, you can use snap:

sudo snap install powershell --classic

After installing, just confirm the installation by checking the version, e.g.:

pwsh --version
PowerShell 7.5.0

Below are some usage examples.


01. Creating a Hello, World!

Create a file with the extension .ps1, for example: main.ps1 and insert the content below:

# Basic program with PowerShell
Write-Host 'Hello, World!'

Comments can be made using the tralha: #

Save and run with the command:

pwsh main.ps1

The output should be: Hello, World!.


02. Downloading a file from the Web:

Example: downloading the file 81by.txt and saving it with the name: logo-ps.png.

It is always necessary to direct to a local file with -OutFile.

Invoke-WebRequest -Uri "http://0x0.st/81by.txt" -OutFile "logo-ps.png"

03. Create and use variables and concatenate:

You need the dollar sign in PHP and Perl style.

$url = "http://0x0.st/81by.txt"
$file = "logo-ps.png"
Write-Host "The url is: " + $url + " and the file: " $file

The concatenation operator: + (plus) is optional, it could also be:

Write-Host "The url is: " $url " and the file: " $file

04. Using if else

Check if the file was saved successfully:

$url = "http://0x0.st/81by.txt"
$file = "logo-ps.png"

Write-Host "The url is: " + $url + " and the file: " $file
Invoke-WebRequest -Uri $url -OutFile $file

if(Test-Path $file 2>$null && (Get-Content $file 2>$null)){
    Write-Host "File was downloaded successfully!"
}else{
    Write-Host "File download failed or file is empty."
}

Also note the use of:

  • && logical operator
  • 2>$url similar to 2>/dev/null
  • In addition to the declarations: Test-Path and Get-Content

05. Creating functions

Adding functions to optimize routines. Everything we did before, but now inside a function that we can change the URL and the file name, always needing to rewrite whenever we want to reuse it for other purposes.

$url = "http://0x0.st/81by.txt"
$file = "logo-ps.png"

Function SaveFile {
    param($url, $file)
        Write-Host "The url is: " + $url + " and the file: " $file
        Invoke-WebRequest -Uri $url -OutFile $file

        if(Test-Path $file 2>$null && (Get-Content $file 2>$null)){
            Write-Host "File was downloaded successfully!"
        }else{
            Write-Host "Failed to download file or file is empty."
        }
}

SaveFile $url $file

Some details in this code:

  • The function parameters need to use the function: param() that is inside the curly braces block
  • In this specific case we need to call Bash style parameters side-by-side the function name: SaveFile $1 $2 and not: SaveFile($1, $2), but in some other cases, like integers we can use the style of most programming languages, for example:
Function IncNumber {
    param($x)
        ++$x
        Write-Host $x
}

IncNumber(2) # 3

In this other example, also note the use of the ++ operator on the left side to increment.


06. Getting content from a file

In this case, we will see the content of the file itself: main.ps1, command style cat:

We comment out the function call: SaveFile and add another line with:

Get-Content "main.ps1"

The output will be:

$url = "http://0x0.st/81by.txt"
$file = "logo-ps.png"

Function SaveFile {
    param($url, $file)
        Write-Host "The url is: " + $url + " and the file: " $file
        Invoke-WebRequest -Uri $url -OutFile $file

        if(Test-Path $file 2>$null && (Get-Content $file 2>$null)){
            Write-Host "File was downloaded successfully!"
        }else{
            Write-Host "Failed to download file or file is empty."
        }
}

#SaveFile $url $file
Get-Content "main.ps1"

07. Interacting with the user

Asking for confirmation to see if he really wants to see the file:

$response = Read-Host "Do you want to view the file? (yes/no)"

if ($response -eq "yes" -or $response -eq "y") {
    Get-Content "main.ps1"
} else {
    Write-Host "You chose not to display the file."
}

Also note the use of the -or operator (equivalent to ||) to determine whether the response is yes or y.


For more information download this PDF and access the links below:


powershell windows csharp


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles