Powershell - type accelerator
To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!
Hello, Accelerator types in Powershell are aliases for .NET types or classes.
Using them can shorten syntax, simplify it and gain performance.
# List accelerators types.[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
## Some examples of use
# Generate an identifier[PSCredential]::New("titi", (ConvertTo-SecureString 'P@ssword' -AsPlainText -Force))# Equivalent to$password = ConvertTo-SecureString 'P@ssword' -AsPlainText -ForceNew-Object System.Management.Automation.PSCredential('titi', $password)
# Easy URL manipulation$url = [uri]"https://www.altf4-formation.fr/powershell"$url.host$url.localpath
# Check an email, convert to type [mailaddress][mailaddress]"test@test.com"
# Check an IP address, convert to type [ipaddress} :[IPAddress]'10.0.0.2'# But we realise that it can also convert this value:[IPAddress]'10.2'# We can counter the problem as follows$ip = '10.0.0.2'($ip -as [IPAddress]).IPAddressToString -eq $ip# "Equivalent" to the following regular expression:"255.25.190.241" -match "^([01]?\d?\d\.|2[0-4]\d\.|25[0-5]\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$"
# Create a custom object[PSCustomObject]@{Name='Guillaume';Age=25}[PSObject]@{Name='Guillaume';Age=25}# Equivalent to :New-Object PSObject -property @{Name='Guillaume';Age=25}
# Define a duration (days, hours, minutes, seconds)$duree = [TimeSpan]::New(2, 10, 30, 40)# Equivalent to :new-timespan -Days 2 -Hours 10 -Minutes 30 -Seconds 40
# Convert to date[DateTime]::Parse('2021-10-12')[DateTime]::Parse('12-10-2021')[DateTime]::Parse('10-12-2021')[DateTime]::Parse('10-12-2021',(get-culture))[DateTime]::Parse('10-12-2021',[cultureinfo]::GetCultureInfo('en-us'))[DateTime]::Parse('10-12-2021').ToString('yyyy_MM_dd')[DateTime]::Parse('10-12-2021').AddDays(8)[DateTime]::Parse('10-12-2021').AddDays(5).AddMonths(-3)[DateTime]::now
## WMI([WMICLASS]'Win32_Process').create("notepad.exe")# Equivalent(Get-WmiObject Win32_Process -List).create("notepad.exe")# orGet-CimClass -ClassName Win32_Process |Invoke-CimMethod -MethodName create -Arguments @{CommandLine='notepad.exe'}
# WMI request to display the process with PID 0 :[Wmi]"Win32_Process.Handle=0"
# Remote request:[WmiClass]"\\remotecomputer\root\cimv2:Win32_Process"
# Use WQL (WMI Query Language) :([WmiSearcher]"SELECT * FROM Win32_Process").Get()
# Create an accelerator type$Accelerator = [PowerShell].Assembly.GetType("System.Management.Automation.TypeAccelerators")# Add an accelerator type$Accelerator::Add('resolve','System.Net.dns')# Use the accelerator type[resolve]::GetHostAddresses('www.google.fr')
Information and list of accelerator types:
https://docs.microsoft.com/fr-fr/powershell/module/microsoft.powershell.core/about/about_type_accelerators?view=powershell-7.1
Ou
https://gist.github.com/atifaziz/3074f15e816cf3566c0600dd6cdccf17
Related links
Powershell - Testing network connectivity and port accessibility
Testing network connectivity and port accessibility with PowershellPowershell - Display network connections (equivalent to netstat)
Display network connections (listening ports, active connections...)Powershell - Testing name resolution (equivalent to nslookup)
Powershell commands to test name resolution (equivalent to nslookup)Powershell - View and manage DNS configuration of network interfaces
Powershell commands to display and manage DNS configuration of network interfacesPowershell - Managing IP configuration of network interfaces
Powershell commands to view and modify the IP configuration of network interfacesPowershell - Managing the status and configuration of network interfaces
Powershell commands to view and modify the status and configuration of network interfaces (disable IPv6, enable/disable an interface)
Follow me on
Support me
Last content
Powershell - Testing network connectivity and port accessibility
Powershell - Display network connections (equivalent to netstat)
Powershell - Testing name resolution (equivalent to nslookup)
Powershell - View and manage DNS configuration of network interfaces
Powershell - Managing IP configuration of network interfaces
Powershell - Managing the status and configuration of network interfaces
Powershell and the Left Hand Side
Powershell - Managing disks, partitions and volumes