TECH BASICS

How to work with PowerShell ?

PowerShell

As per Microsoft,

.NET Core is a free, cross-platform, open-source developer platform for building many different types of applications.

Using PowerShell

> docker run --rm -it mcr.microsoft.com/azure-powershell
PS /> $PSVersionTable
PS /> (Get-Command).Count
PowerShell Version Details
PS C:\> Get-ExecutionPolicy
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

CMDLETS

Get-Verb
Get-Alias

PowerShell Module

PS /> Get-Module
PS /> Get-Module -ListAvailable
PS /> $env:PSModulePath
PS /> $env:PSModulePath -split ";"
# if you want to add location of any path (but this will be for session)PS /> $env:PSModulePath = $env:PSModulePath + ";D:/MyPWSHModule"
PS C:\Users\Arun> $current = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')
PS C:\Users\Arun> $current -split ";"
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files (x86)\Microsoft Azure\Powershell
PS C:\Users\Arun> $newpath = $current + ';C:\MyNewPath'PS C:\Users\Arun> [Environment]::SetEnvironmentVariable("PSModulePath", $newpath, 'Machine')
PS /> Get-Command -Module PSReadLine
PS /> Get-Command -Module PSReadLine -Verb Get

Good to know Commands

# used to search installed commands
PS /> Get-Command -Verb get -Noun *TIME*
PS /> Get-Command -Name *service*
PS /> Get-Help -Name *TIME*
PS /> help Get-Process | more
Objects are everywhere in PowerShell. PowerShell uses objects as its output
PS /> Get-Process | Get-Member 
PS /> Get-Process | Get-Member -MemberType property
PS /> Get-Process | Get-Member -MemberType Method
# List all property and methods 
PS C:\Users\Arun> Get-Service | Get-Member

PS C:\Users\Arun> Get-Service -Name Tomcat8
Status Name DisplayName
------ ---- -----------
Running Tomcat8 Apache Tomcat

PS C:\Users\Arun> (Get-Service -Name Tomcat8).Status
Running# Using MethodPS C:\Users\Arun> (Get-Service -Name Tomcat8).Stop()Running
PS /> Get-Process | Select-Object -Property 'Id','StartTime'
PS /> Get-Process | Select-Object -Property 'Id','StartTime','HandleCount' | Where-Object -FilterScript { $_.Id -eq "1"  } | Format-Table -AutoSize
PS /> Invoke-History
PS /> Get-History | Out-File history.txt

Providers

PS /> Get-PSProvider
Get-PSProvider
Get-WMIObject Win32_Service -filter "startname='DOMAIN\\SVC_ACNT_NAME'" | Select-Object -Property Name,Status,State

WMI and CIM

Ex:PS C:\Users\Arun> Get-CimInstance -ClassName Win32_BIOS | Select-Object -Property SerialNumberPS C:\Users\Arun> Get-WmiObject -Class Win32_Process | Format-List -Property PSComputerName, Name, ExitCode, Name, ProcessID, StartMode, State, StatusPS C:\Users\Arun> (Get-WmiObject -Class Win32_Service -Filter "name='WinRM'" -ComputerName Server01).StopService()

PowerShell Package Management

PS /> Get-PackageProvider
PS /> Get-Command -Module PackageManagement
PS /> Get-PSRepositoryName       InstallationPolicy   SourceLocation
---- ------------------ --------------
PSGallery Untrusted https://www.powershellgallery.com/api/v2

In quest of understanding How Systems Work !

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store