Skip to main content

PowerShell Tricks

Power shell scripts

SESSION STATE ELEVATION

Get Child Item Count

Get-childItem -Path "master:\sitecore\media library\Sites\" -Recurse | Where-Object { $_.TemplateName -eq "Pdf"} | Measure-Object

Objects

PS C:\> $files = dir
PS C:\> $files
PS C:\> Get-Service | Out-GridView

Sorting

PS C:\> $files|sort -Property length
PS C:\> $files|sort -Property length -Descending

Filtering

$files| Where-Object Length -gt 212569458
$files | Where-Object Name -eq "InExpEditor.mp4"
$files | Where-Object Name -Like "V*"
$files | Where-Object {$_.Length -gt 200000000}
$files | Where-Object {($_.Length -gt 200000000) -and ($_.Name -like "D*")}

Loops

1..10 | foreach{ " This number is " + $_ }
1..10 | foreach{ if($_%2){"$_ is odd number "} }

Arrays

$strComputers = @("computer1","computer2","computer3")
$strComputers | foreach{ $_ + " - " + $_.length }

Hash tables

$employees  = @{"Emp1"=111;"Emp2"=222;"Emp3"=333 }
$employees["Emp1"]
$employees.Remove("Emp3")

Formatting Objects

$files | Format-Wide
$files | Format-List
$files | Format-List -Property Name
$files | Format-Table
Get-Process|Format-Table -Property Id, ProcessName,Company -AutoSize
Get-Process| sort -Property Company | Format-Table -Property Id, ProcessName,Company -GroupBy Company -AutoSize
Get-Process | Out-File C:\temp\process.txt
notepad C:\temp\process.txt
Get-Process | ConvertTo-Html | Out-File C:\temp\process.html
Invoke-Expression C:\temp\process.html
Get-Process | Export-Csv C:\temp\process.csv
Invoke-Expression C:\temp\process.csv

Import Objects

$names = Import-Csv C:\temp\census1000.csv
$names | Format-Table
$names| Sort-Object pcthispanic -Descending | Format-Table -Property Name,pcthispanic
($names| Sort-Object pcthispanic -Descending)[0].Name
$names | Sort-Object pcthispanic -Descending | select -First 10

Functions

function Add-Numbers
{
    param([int]$num1,[int]$num2)

    return $num1 + $num2
}

Add-Numbers 1 2

$result  = Add-Numbers -num1 4 -num2 5
$result

PowerShell ISE

[System.Net.Dns]::GetHostEntry("10.191.92.6").HostName
[System.Net.Dns]::GetHostByAddress("10.191.92.6").Hostname
[System.Net.Dns]::GetHostAddresses(“nlgutcmswsvm01”).IPAddressToString

Find the thumbprint of the certificate

dir cert: -Recurse | Where-Object { $_.Thumbprint -like "*XXXXXXXXXXXXXXXXXXXXX*" }

Check the certificate purpose

$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Thumbprint -match "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
foreach($key in $cert.Extensions){if('EnhancedKeyUsages' -in $key.psobject.Properties.Name){ $key.EnhancedKeyUsages.FriendlyName }}

Get CPU Cores

Get-WmiObject -Class Win32_Processor | Select-Object -Property Name, Number*

Get IP Address

Get-NetIPAddress -AddressFamily IPV4

Get .Net Framework Version

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where { $_.PSChildName -Match '^(?!S)\p{L}'} | Select PSChildName, version

Comments

Popular posts from this blog

Sitecore Item Bucket

Item buckets How to Create Item Buckets in Sitecore CMS How to Create Custom Item Bucket Rules in Sitecore CMS How to Configure Item Bucket Paths in Sitecore CMS How to Make Custom Bucket Structure in Sitecore - Brimit Blog Bucketing Strategies Item Buckets and URLs How to use Sitecore Bucketing Feature in powerful way?

Sitecore SaaS( XM Cloud ) Migration

Sitecore XM Cloud vs Sitecore XP vs Sitecore XM Sitecore XM Cloud vs Sitecore XP vs Sitecore XM | Fishtank Consulting (getfishtank.com) Sitecore XM Cloud v Sitecore XP v Born-Headless CMS - Oshyn Sitecore XM Cloud What Is Sitecore XM Cloud: What We Know (Updated) | Fishtank Consulting (getfishtank.com) XM Cloud FAQ (sitecore.com) Sitecore Architect’s Guide to SaaS Migration – XP Marketing Automation – St-Cyr Thoughts (jasonstcyr.com) Sitecore Next JS Guide Introduction – Sitecore Next.js Guide (hakmeng.com)