PowerShell Tricks 💡
Arun Kumar Singh
Posted on March 19, 2021
Finding Version of your powershell
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.1490
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.1490
List AD Groups which you are part of :
PS C:\> $id = [Security.Principal.WindowsIdentity]::GetCurrent()
PS C:\> $groups = $id.Groups | foreach-object {$_.Translate([Security.Principal.NTAccount])}
PS C:\> $groups | select *
List Processes
PS C:\> Get-Process | Sort-Object CPU -descending | Select-Object -first 1
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
2593 97 154056 174068 458.39 18020 1 chrome
Filter Example
Get-Process | Select-Object -Property 'Id','StartTime','HandleCount' | Where-Object -FilterScript { $_.Id -eq "1" } | Format-Table -AutoSize
To list what modules are loaded in current powershell session
PS C:\> Get-Module
ModuleType Version Name ExportedCommands
--------------- ------- ---- ----------------
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}
# List Available Modules
PS C:\> Get-Module -ListAvailable
Locate your command
PS C:\> Get-Command -Name *service*
CommandType Name Version Source
---------------- ---- ------- ------
Alias Get-ASRServicesProvider 0.2.4 AzureRM.RecoveryServices.SiteRecovery
Alias Get-AzureRmRecoveryServicesAsrNotificationSetting 0.2.4 AzureRM.RecoveryServices.SiteRecovery
Alias Get-AzureRmRecoveryServicesAsrVaultSettings 0.2.4 AzureRM.RecoveryServices.SiteRecovery
Alias Get-AzureRmRecoveryServicesBackupProperties 4.1.2 AzureRM.RecoveryServices
Alias New-AzureRmDataFactoryV2LinkedService 0.5.3 AzureRM.DataFactoryV2
Using Get-Help
PS C:\> Get-Help -Name *TIME*
Name Category Module Synopsis
--------- -------- ------ --------
New-TimeSpan Cmdlet Microsoft.PowerShell.U... ...
Get-TimeZone Cmdlet Microsoft.PowerShell.M... ...
Set-TimeZone Cmdlet Microsoft.PowerShell.M... ...
PS C:\> Get-Help Get-TimeZone
NAME
Get-TimeZone
SYNTAX
Get-TimeZone [[-Name] <string[]>] [<CommonParameters>]
Get-TimeZone -Id <string[]> [<CommonParameters>]
Get-TimeZone -ListAvailable [<CommonParameters>]
To find out the information attached with an object use “Get-Member” cmdlet.
PS C:\> Get-Service | Get-Member
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
--------- ---------- ----------
Name AliasProperty Name = ServiceName
RequiredServices AliasProperty RequiredServices = ServicesDependedOn
Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs)
Close Method void Close()
Continue Method void Continue()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
ExecuteCommand Method void ExecuteCommand(int command)
Repository from where packages are coming
PS C:\> Get-PSRepository
Name InstallationPolicy SourceLocation
--------- ------------------ --------------
PSGallery Untrusted https://www.powershellgallery.com/api/v2
Grep Examples
PS C:\Temp> Select-String -Path *.yaml -Pattern "kind" | Select Filename, LineNumber, Line, Path | Format-Table
Filename LineNumber Line Path
------------- ---------- ---- ----
lgl.yaml 4 kind: ConfigMap C:\Temp\lgl.yaml
lgl.yaml 19 kind: ConfigMap C:\Temp\lgl.yaml
lgl.yaml 33 kind: Service C:\Temp\lgl.yaml
lgl.yaml 51 kind: Service C:\Temp\lgl.yaml
Place output of command in a file
Get-Process | Select-Object -Property 'Id','StartTime' | Out-File a.txt
Select-String -Path *.yaml -Pattern "kind" | Select Filename, LineNumber, Line, Path | Format-Table | Export-Csv -path output.csv -NoTypeInformation
Path
$Env:Path += ";c:\temp"
Set-Item -Path Env:Path -Value ($Env:Path + ";C:\Temp")
Command History
PS C:\Temp> Get-History
Id CommandLine
-- -----------
1 cd c:/
2 $id = [Security.Principal.WindowsIdentity]::GetCurrent()
3 $groups = $id.Groups | foreach-object {$_.Translate([Security.Principal.NTAccount])}
4 $groups | select *
Copy Data
Copy-Item -Filter *.yaml -Path c:\temp -Recurse -Destination D:\temp\
I will keep updating this post if I find something useful. Thanks
💖 💪 🙅 🚩
Arun Kumar Singh
Posted on March 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.