Leverage PowerShell on any OS ‐ PowerShell useful commands - Davz33/tutorials GitHub Wiki

Introduction

If you're running the most recent versions of Microsoft Windows, PowerShell will be already included. If not, you can install PS via (ref):

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Delete the the Microsoft repository GPG keys file
rm packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

In any case, why bother?
I found PS to be an easier, less spaghetti-code prone and clearer medium to run operation via shell as replacing text in string, manipulate content of text files, run loops that move or manipulate the file-system. Here, I'm referring to a comparable functionality in bash, or zsh (Unix-like shells).

PowerShell receives substantial updates, and that explains why a lot of spaghettification has been dropped.

String Manipulation

Replace substring in string

$TestString = "test=keep this, but not this."

$NewString = $TestString -replace "<findthis>" -replace "<replacewiththis>"

Getting the nth line from a string

Remember that newlines are simply \n encoded, so are other paragraph features of common text files. This means a single string variable can contain a whole MS Word-compatible text, and be ready to get printed onto it.

($Text -split '\n')[<stringnumber_start_from_0>]

Text Files and Files I/O

Get text content of a file

$Text = Get-Content -Path .\Process.txt

Write variable / string to file

Out-File -FilePath .\Process.txt -InputObject $Text
⚠️ **GitHub.com Fallback** ⚠️