Skip to content

Instantly share code, notes, and snippets.

Function Write-Log {
param(
$Message,
$logPath,
$Color = "Yellow"
)
Write-Host "$Message" -ForegroundColor $Color
If ($logPath -ne $null)
{
@milesgratz
milesgratz / Use-Proxy.ps1
Last active April 12, 2017 15:26
Use-Proxy function suggestion (#powershell irc-bridge)
Function Use-Proxy {
$proxy = 'http://my.proxy.server:80/'
$WebRequest_params = @{
Uri = 'http://www.example.com'
Method = 'Head'
DisableKeepAlive = $true
UseBasicParsing = $true
}
Try {
@milesgratz
milesgratz / Get-NetworkAdapter-PSv2.ps1
Created April 12, 2017 18:43
Get-NetworkAdapter (PowerShell v2 example)
$NetAdapterList = (gwmi -Query "SELECT * FROM Win32_NetworkAdapter")
$NetAdapterConfigList = (gwmi -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'")
$Results = @()
foreach ($NetAdapter in $NetAdapterList){
$NetAdapterConfig = $NetAdapterConfigList | Where-Object { $_.Index -eq $NetAdapter.DeviceID }
If ($NetAdapterConfig -ne $null)
{
$Object = New-Object PSCustomObject
@milesgratz
milesgratz / find-recursive-files-move-to-parent.ps1
Created April 13, 2017 11:41
Find recursive text files and move to parent folder
# Define parent folder
$Parent = "C:\temp\Parent"
# Search for text files recursively
$Files = Get-ChildItem -Path $Parent -Filter *.txt -Recurse
# Loop through files, moving to Parent folder
foreach ($File in $Files)
{
Try
@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)
{
$_
@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 / 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 / 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 / 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 / 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