DEV Community

Arun Kumar Singh
Arun Kumar Singh

Posted on

PowerShell Tricks πŸ’‘

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
Enter fullscreen mode Exit fullscreen mode

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 *
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Filter Example

Get-Process | Select-Object -Property 'Id','StartTime','HandleCount' | Where-Object -FilterScript { $_.Id -eq "1"  } | Format-Table -AutoSize
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>]


Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Repository from where packages are coming

PS C:\> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
---------                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Path

$Env:Path += ";c:\temp"
Set-Item -Path Env:Path -Value ($Env:Path + ";C:\Temp")

Enter fullscreen mode Exit fullscreen mode

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 *

Enter fullscreen mode Exit fullscreen mode

Copy Data

Copy-Item -Filter *.yaml -Path c:\temp -Recurse -Destination D:\temp\
Enter fullscreen mode Exit fullscreen mode

I will keep updating this post if I find something useful. Thanks

more on PowerShell

Top comments (0)