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
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.
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!.
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"
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
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 operator2>$url
similar to 2>/dev/null
Test-Path
and Get-Content
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:
param()
that is inside the curly braces blockSaveFile $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.
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"
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: