Skip to content

Instantly share code, notes, and snippets.

@huynhbaoan
Created November 30, 2022 10:25
Show Gist options
  • Save huynhbaoan/d4a6a09c61258fc8e8b76aebfc63d91d to your computer and use it in GitHub Desktop.
Save huynhbaoan/d4a6a09c61258fc8e8b76aebfc63d91d to your computer and use it in GitHub Desktop.
Different from bash, PowerShell is object-orriented. Understanding objects are necessary to utilize PowerShell
Get-NetIPAddress -AddressFamily IPV4 -AddressState Preferred -IncludeAllCompartments | Get-Member
TypeName: Microsoft.Management.Infrastructure.CimInstance#ROOT/StandardCimv2/MSFT_NetIPAddress
Name MemberType Definition
---- ---------- ----------
ifIndex AliasProperty ifIndex = InterfaceIndex
Clone Method System.Object ICloneable.Clone()
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.St...
GetType Method type GetType()
Address Property string Address {get;set;}
AddressOrigin Property uint16 AddressOrigin {get;set;}
AddressType Property uint16 AddressType {get;set;}
ToString ScriptMethod System.Object ToString();
AddressState ScriptProperty System.Object AddressState {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Address...
PrefixOrigin ScriptProperty System.Object PrefixOrigin {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.PrefixO...
Store ScriptProperty System.Object Store {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Store]($this.P...
SuffixOrigin ScriptProperty System.Object SuffixOrigin {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.SuffixO...
Type ScriptProperty System.Object Type {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetIPAddress.Type]($this.PSB...
@huynhbaoan
Copy link
Author

huynhbaoan commented Nov 30, 2022

Get-Member is the key to understanding PowerShell objects

Get-NetIPAddress -AddressFamily IPV4 -AddressState Preferred -IncludeAllCompartments | Get-Member

Then Format-List -Property * will help with properties

Get-WMIObject win32_operatingsystem | Format-List -Property *

PSComputerName : MyComputer
Status : OK
Name : Microsoft Windows 10 Pro|C:\Windows|\Device\Harddisk0\Partition3
FreePhysicalMemory : 21066540
FreeSpaceInPagingFiles : 4976008
FreeVirtualMemory : 25714872
__GENUS : 2
__CLASS : Win32_OperatingSystem
__SUPERCLASS : CIM_OperatingSystem
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_OperatingSystem=@

Access object properties by (.)

(Get-NetIPAddress -AddressFamily IPV4 -AddressState Preferred -PrefixOrigin Dhcp -IncludeAllCompartments).Ipaddress

Store output to a variable

$object=(Get-NetIPAddress -AddressFamily IPV4 -AddressState Preferred -PrefixOrigin Dhcp -IncludeAllCompartments).Ipaddress

Retrieve a variable value

$object

Check type

$object.GetType()

Use ScriptMethod (ToString)

$text=$object.ToString()
$text.GetType()

@huynhbaoan
Copy link
Author

Work with strings

Use ScriptMethod (ToString)

$text=$object.ToString()
$text.GetType()

From original strings

(Get-WMIObject win32_operatingsystem).Name

Microsoft Windows 10 Pro|C:\Windows|\Device\Harddisk0\Partition3

Now, split the strings into 3 strings

(Get-WMIObject win32_operatingsystem).Name -split "|"

Microsoft Windows 10 Pro
C:\Windows
\Device\Harddisk0\Partition3

Now, split into fewer strings, only 2

(Get-WMIObject win32_operatingsystem).Name -split "|", 2

Microsoft Windows 10 Pro
C:\Windows|\Device\Harddisk0\Partition3

Now, get only the 1st substring

((Get-WMIObject win32_operatingsystem).Name -split "|", 2)[0]

Microsoft Windows 10 Pro

@huynhbaoan
Copy link
Author

huynhbaoan commented Dec 19, 2022

Run PowerShell script, bypass restriction policy

powershell -ExecutionPolicy Bypass -File

Run PowerShell script, waiting command promt

Read-Host -Prompt "Press Enter to exit"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment