PowerShell

Set-ExecutionPolicy Unrestricted -Force

You try to run your script

./your_script.ps1

And you get

.\your_script.ps1 : File C:\Users\Andrei\your_script.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + .\your_script.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

You need to open PowerShell as administrator and run

Set-ExecutionPolicy Unrestricted -Force

And run it once more

./your_script.ps1

If you cannot run PowerShell in administrator mode. For example, if you use the terminal in Visual Studio Code

Run

Set-ExecutionPolicy -Scope CurrentUser Unrestricted -Force

You can switch PowerShell to Administrator mode right from a regular PowerShell console with the command

start-process powershell -verb runas

Checking the version

$PSVersionTable

Name Value ---- ----- PSVersion 5.1.19041.2364 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.2364 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

Installing PowerShell

It may come in handy for upgrading to a newer version.

With winget. First it is recommended to check the available versions

winget search Microsoft.PowerShell

The `msstore` source requires that you view the following agreements before using. Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction The source requires the current machine's 2-letter geographic region to be sent to the backend service to function properly (ex. "US"). Do you agree to all the source agreements terms? [Y] Yes [N] No: Y Name Id Version Source ------------------------------------------------------- PowerShell Microsoft.PowerShell 7.3.3.0 winget PowerShell Microsoft.PowerShell.Preview 7.4.1.0 winget

The installation itself

winget install --id Microsoft.Powershell --source winget

Found PowerShell [Microsoft.PowerShell] Version 7.3.3.0 This application is licensed to you by its owner. Microsoft is not responsible for, nor does it grant any licenses to, third-party packages. Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.3.3/PowerShell-7.3.3-win-x64.msi ██████████████████████████████ 101 MB / 101 MB Successfully verified installer hash Starting package install... Successfully installed

PowerShell 7 does not install as a replacement for the regular Windows PowerShell but as additional software.

To open it, type PowerShell 7 in the search

Checking the version is usually not needed, since the version is shown before the first command-line prompt. Nevertheless, you can run the standard command

$PSVersionTable

Name Value ---- ----- PSVersion 7.3.3 PSEdition Core GitCommitId 7.3.3 OS Microsoft Windows 10.0.19045 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

Banner image
Network
Open ports
Open a port
Get-NetIPConfiguration: network information
Allow RDP connections
OpenSSH server status
Start sshd
Users
List of users
Create a user
Commands
Get-Content -Tail: View the end of a file (tail equivalent)
Get-FileHash: Check the checksum of a file (md5sum equivalent)
Select-String: Select a line by pattern (grep equivalent)
Stop-Process: Stop a process (kill equivalent)
Create a new file (touch equivalent)

Article author: Andrei Olegovich