Info
Powershell Powershell, bring more helpful and memorable command which can reuse on next time π π π
Extract zip file
You can use Expand-Archive for extract ZIP file, for example
Expand-Archive .\translators-main.zip -DestinationPath .The command will extract the translators-main.zip file in current folder
Restart wsl to claim memory
You can use --shutdown flag to restart the wsl machine or you can setup .wslconfig on PATH $env:USERPROFILE, read more: Advanced settings configuration in WSL. More about scenarios you can read in StackOverFlow - How can I reduce the consumption of the vmmem process?
Note
Shutdown to reclaim resource
# Shutdown WSL to reclaim resource
wsl --shutdownInfo
Set new configuration
# Update new info for wsl2
[wsl2]
memory=3GB # Set the limit memory can give for wsl# Try shutdown after change configuration
wsl --shutdownCommon wsl command
Official documentation: Basic commands for WSL and Remove WSL
# Update wsl
wsl --update
 
# Check the list subsystem running
wsl --list --running
 
# Set default linux distribution
wsl --set-default <Distribution Name>
 
# Shutdown the wsl
wsl --shutdown
 
# Terminate linux distribution
wsl --terminate <Distribution Name>
 
# List distribution you have on Window machine
wsl --list
 
# List available Linux distributions
wsl --list --online
 
# Set WSL version to 1 or 2
wsl --set-version <distribution name> <versionNumber>
 
# Export a distribution
wsl --export <Distribution Name> <FileName>
 
# Import a distribution
wsl --import <Distribution Name> <InstallLocation> <FileName>Invoke-WebRequest alias of wget & curl
Reference: StackExchange - PowerShell equivalent of curl - Invoke-WebRequest
You can use curl alias of command Invoke-WebRequest
# Use on curl style
curl -Uri "https://www.example.com/myfile.txt" -OutFile myfile.txt
 
# Use on altenative
Invoke-WebRequest -Uri "https://www.example.com/myfile.txt" -OutFile myfile.txtBecause the powershell output work really special, object-type I think so. Thus, you can use select-object to get the what actually you want, such as Content
# Output not raw string
Invoke-WebRequest -Uri https://www.google.com | Select-Object 'Content'
For expand the raw string, you can use  -ExpandProperty flag like example
Invoke-WebRequest -Uri https://www.google.com | Select-Object -ExpandProperty 'Content'
Go to the administrator
Use can use start-process with -verb flag on runas mode to change your shell to Administrator role
# Open Windows Terminals (New version if you install)
Start-Process wt -Verb runAs
 
# Open powershell in Terminal
Start-Process powershell -Verb runAsBut on the currently, Windows just ready to release sudo command for help you doing super user command can do without open terminal with Administrator. Read more at: Sudo for Windows
Reload $PROFILE
When you want to reload or apply plugin which you put on your $PROFILE, you can make it straightway with command
. $PROFILEEnable Hyper-V service
Info
Information about Issue StackOverFlow - How to disable Hyper-V in command line?]
Command Prompt (CMD)
In anΒ elevatedΒ Command Prompt write this
# To disable
bcdedit /set hypervisorlaunchtype off
# To enable
bcdedit /set hypervisorlaunchtype auto Restart to take effect or you can do it with command
# Restart in one minute left
shutdown /r
 
# Restart immediately
shutdown /r /t 0
 
# Restart after time
shutdown /r /t TIMEPowershell
Run in administrator before execute this
To disable
# To disable
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
 
# To enable
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
 And restart or use command
# Restart in one minute left
shutdown /r
 
# Restart immediately
shutdown /r /t 0
 
# Restart after time
shutdown /r /t TIMEGet execute policy
Try to list all permission of powershell of currently user
Get-ExecutionPolicy -ListView all permission of user
Use whoami command to view
whoami /allSet and change permission of file
Documentation: How to change file permissions on Windows via powershell / cmd?
To change and set permission of file or folder in powershell, you can use Get-AclΒ andΒ Set-Acl
# Copy permission from dog --> cat
Get-Acl -Path "C:\Dog.txt" | Set-Acl -Path "C:\Cat.txt"Or we can use icacls to handle with same situation. Read more tutorial and explanation
# Β grant the group FileAdmins 'Delete' and 'Write DAC' permissions to C:\demo\example
icacls "C:\demo\example" /grant:r FileAdmins:(D,WDAC)If you want to create 400 permission for your ssh-key or readonly file for currently user, you can use
# Give current user explicit read-permission
icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
# Disable inheritance and remove inherited permissions
icacls.exe $path /inheritance:rCreate new file
Explore more about methodology: How to Use PowerShell to Create a File?
There is more of stuff way for create new file, and one of popular is use New-Item
New-Item -Path "C:\Logs\NewLog.txt" -ItemType FileIn another, you can use Out-File or Set-Content
# Use Out-File
"Hello World" | Out-File -FilePath "C:\Logs\NewLog.txt"
 
# Use Set-Content
Set-Content -Path "C:\Logs\NewLog.txt" -Value "Hello World"Find the string in documentation
If you want to find the same idea of grep in Powershell, you can try with. Read more at PowerShell equivalent to grep -f
# Use Select-String with regex pattern
ipconfig --help | Select-String -Pattern "/all"
 
# Use findstr function.
# Look the help of function
findstr /?
# Try to find include str
ipconfig --help | findstr /I all
# Uses specified string as a literal search string.
ipconfig --help | findstr /C:"/all"Command can be used
Get-Help
Use theΒ Get-HelpΒ cmdlet to display the syntax of any PowerShell cmdlet
# Basic
Get-Help Get-Service
 
# List example
Get-Help Get-Service -Examples
 
# Search onl
Get-Help Get-Service -OnlineGet-Service
Helpful to know what services are installed on the system
# Basic
Get-Service
 
# Pick display name colume
Get-Service | select DisplayName
 
# Find some service with condition
Get-Service | Where-Object {$_.status -eq "stopped"}Get-EventLog
Actually use PowerShell to parse your machineβs event logs using the Get-EventLog cmdlet
# View Application event log
Get-EventLog -Log "Application"
 
# View Security event log (Admin)
Get-EventLog -Log "Security"Get-Process
Getting a list of available services, itβs often useful to be able to get a quick list of all the currently running processes
# Basic
Get-Process
 
# Get full process (parrent and child)
Get-Process -Name chrome
 
# You can use Get-Process with combine Stop-Process
Get-Process | Where-Object { $_.Name -eq "myprocess" } | Select-Object -First 1 | Stop-ProcessFormat-Table
Β Used to format any output as a table
# Format list of process with fit-size
Get-Process | Format-Table -Property Name, CPU, Memory -AutoSizeFormat-List
Output properties as a list, each on a new line
# gets services and lists each individually
Get-Service | Format-ListWhere-Object
Where-ObjectΒ is one of the most important cmdlets to know, as it enables you to take a dataset and pass it further down your pipeline for filtering
Get-Service | Where-Object {$_.status -eq "stopped"}For-Each
The ForEach-Object cmdlet performs an operation against every item in a specified group of input objects.
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}Compare-Object
Useful to be able to compare two objects directly
Compare-Object "as" "ax"Select-Object
The Select-Object cmdlet selects the specified properties of a single object or group of objects.
Get-Process | Sort-Object name -Descending | Select-Object -Index 0,1,2,3,4Get-Member
One quality that makes PowerShell so versatile is that almost everything in PowerShell is an object consisting of a name, methods, and properties
Get-Service | Get-Member
Get-ChildItem
Β Get-ChildItemΒ returns all the items in one or more containers
# Basic
 Get-ChildItem .\content\
 
# Filter and include with folder
Get-ChildItem C:\Temp\* -Recurse -Include *taco*.txtOut-GridView
Β PowerShell output returns to the console window. However, if you need to interact with the output, you can use theΒ Out-GridViewΒ cmdlet
Get-Process | Out-GridViewBase64 Decode and Encode
You can follow this question at StackOverFlow - How to decode a Base64 string?
Convert TO Base64
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('MotΓΆrhead'))Convert FROM Base64
[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String('TW90w7ZyaGVhZA=='))