Skip to content

Instantly share code, notes, and snippets.

@milesgratz
milesgratz / Update-machine-certificate-private-key-permissions.ps1
Created May 31, 2019 17:40
PowerShell example of updating machine certificate private key permissions (CAPI vs CNG)
# Get cert on local computer by thumbprint
$thumbprint = '89D3FC64B6405E161EDC7A4CF14E111F5F6895AA'
$Cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $thumbprint }
###################################################
# Manage private key of CAPI cert
###################################################
# Find private key
$privKey = $Cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
@milesgratz
milesgratz / parse_powershell_ise_theme.ps1
Created June 15, 2017 13:52
Parse PowerShell_ISE theme for RGB values
$File = 'C:\Users\miles.gratz\desktop\Dark Console Light Editor default.StorableColorTheme.ps1xml'
$Content = Get-Content $file
$Results = @()
$Index = 0
foreach ($line in $content)
{
if ($line -match "\<string\>")
{
$key = (($line -split "<string>")[1] -split "</string>")[0]
$object = New-Object PSCustomObject
@milesgratz
milesgratz / Replace-individual-rows-in-CSV.ps1
Created June 12, 2017 18:44
Replace an individual rows in a CSV by indexing
<#
Replace individual rows in a CSV
(if there are no multiline rows)
Define example input file
City Zipcode
---- -------
Phoenix, AZ 85001
Little Rock, AR 72201
@milesgratz
milesgratz / Run-CustomFunction-DoubleHop.ps1
Created June 12, 2017 16:39
Custom function to start Invoke-Command "jobs" on remote systems ... but with double hop consideration
$FirstHop = "Server1"
$SecondHop = "Server2"
$Cred = Get-Credential
Function Run-CustomFunction {
param(
$FirstHop,
$SecondHop,
$Cred
)
@milesgratz
milesgratz / Run-CustomFunction.ps1
Last active June 12, 2017 16:09
Custom function to start Invoke-Command "jobs" on remote systems
Function Run-CustomFunction {
param($ComputerName)
# Create a job for tracking results
Start-Job -Name $ComputerName -ScriptBlock {
# Run the command on the remote Computer
Invoke-Command -ComputerName $args[0] -ScriptBlock {
Get-ChildItem "C:\Temp"
}
} -ArgumentList $ComputerName
@milesgratz
milesgratz / Replace-irregular-chars.ps1
Last active April 23, 2017 06:06
Replace non-alphanumeric characters in folder of files
# Potential input
# -------------------------------------
# Test File With Spaces.txt
# Test_File_With_Underscores.txt
# Weird_Chars##$%@1.txt
$badFiles = Get-ChildItem "C:\temp\irregular files"
foreach ($file in $badFiles)
{
@milesgratz
milesgratz / Get-Registry-Report.ps1
Last active April 20, 2017 12:09
Recursively get all keys, subkeys, and values from specific Registry Path
#---------------------------------------------------
# Let's find all registry keys beneath specific path
#---------------------------------------------------
# Define root of registry query
$topLayer = "HKLM:\SOFTWARE\Microsoft\.NETFramework"
# Define empty array for all layers
$allLayers = @()
$allLayers += $topLayer
@milesgratz
milesgratz / Expand-and-install-cabs.ps1
Created April 17, 2017 17:50
Example to expand and install driver cabs
$CabPath = "C:\temp"
$CabFiles = Get-ChildItem $CabPath\*.cab
# Expand Cabs into folder based on their Name
foreach ($Cab in $CabFiles){
$Directory = New-Item -Path $CabPath\$($Cab.BaseName) -ItemType Directory -Force
expand $Cab.FullName -F:* $Directory.FullName
}
# Install drivers inside each cab
@milesgratz
milesgratz / Stale Computers example.ps1
Last active April 14, 2017 17:38
Sample Stale Computers report (and disable)
# Search for stale computers
$StaleComputers = Get-ADComputer -Filter {
Enabled -eq $true -and OperatingSystem -like "*Windows*" -and OperatingSystem -notlike "*Server*"
} -Properties LastLogonDate,Description,Location,OperatingSystem,CanonicalName |`
Where-Object { ((Get-Date) - ($_.LastLogonDate)).TotalDays -gt 45 }
# Export results to C:\temp
$StaleComputers | Export-Csv C:\temp\Stale-Computers.csv -NoTypeInformation
# Uncomment to disable
@milesgratz
milesgratz / using-match-array-of-objects.ps1
Created April 14, 2017 12:50
Using -match with array of objects
$Source = "txt","csv","pdf","xlsx","docx"
$Find = "csv","pdf"
$Source | ForEach-Object {
# does not work
# -match cannot find item in array
If ($_ -match $Find)
{
$_