Category: Codes

PowerShell: Some Tricks in Using the Here-String to Declare Blocks of Code

Trigger COMMAND CLI from within PowerShell # Fastest way to list all files in a…

PowerShell: Remove an A-Host Record within Active Directory Integrated DNS Domain

Warning: this code is NOT 'production ready'. Please review and test on DEV environments carefully…

PowerShell: Add Accounts into Local Administrators Group

# addAccountToLocalAdmins.ps1# Requires: PowerShell Version 4.0+$accountToAdd='ServerAdmins'$groupName="Administrators"$servers=@( "CONCU1", 'CONCU2', 'CONCU100', 'CONCU80665', 'CONCU6547354', 'CONWHAT989', 'CONCU3')$servers|%{ $session=new-pssession $_…

PowerShell: Special Parameter datatype [switch]

# Demo of a simple function to turn lights on and offfunction setLight{ param( [switch]$on,…

PowerShell: Check DotNet Framework on Windows

function checkDotNetVersions{ # General info: # - Powershell 2.0 latest SSL version support is TLS…

PowerShell: Fix Clustered Disk Errors

# fixClusterDiskErrors.ps1function selectClusterName{ param($domainName=$env:USERDNSDOMAIN) write-host "Now scanning $domainName for all available cluster names. This may…

PowerShell: Generate Report of Users and Computers That Have Not Logged On for X Days

# AccountsNotLoginXDays.ps1# Set days$lastLogonDaysExceeding = 120# Gather Users$daysRange = (get-date).adddays(-$lastLogonDaysExceeding)$users=Get-ADUser -properties * -filter {(enabled -eq…

PowerShell: Change Windows Autolock

# Disable_AutoLock.ps1function changeAutoLock{ param ( [Switch]$disable, [Switch]$enable ) $registryHive = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization" $registryKey="NoLockScreen" $keyItem = Get-ItemProperty…

Windows 2012: How To Install DotNet Framework 2.0 & 3.0

If there're HTTPS/TLS issues, run this function: https://blog.kimconnect.com/powershell-enable-tls-1-2-on-legacy-servers/ Toggle WSUS registry key to off prior…

PowerShell: Windows Defender Controlled Folder Access

# Default Setting of Windows Defender:PS C:\Windows\system32> get-mppreferenceAttackSurfaceReductionOnlyExclusions :AttackSurfaceReductionRules_Actions :AttackSurfaceReductionRules_Ids :CheckForSignaturesBeforeRunningScan : FalseCloudBlockLevel : 0CloudExtendedTimeout…

PowerShell: An Exercise in Calculating Checksums

$out = new-object byte[] 1073741824; #1GB(new-object Random).NextBytes($out);[IO.File]::WriteAllBytes($dummyFile, $out);Measure-command{$hash=jacksum -a crc8 -x $dummyFile}write-host $hash# New ServerPS…

PowerShell: Enable TLS 1.2 on legacy servers

# Enable TLS 1.2 on legacy servers function enableTls12{ $windowsVersionNumber=[System.Environment]::OSVersion.Version.Major; #$windowsName=(Get-CimInstance -ClassName Win32_OperatingSystem).Caption; #The term…

PowerShell: Windows File Migration Tool Using VSS & FastCopy

Update: FastCopy doesn't properly update NTFS permissions of parent directories nor files if those items…

PowerShell: Hobocopy Backup Software

# hobocopy-backup.ps1# Note: this is a quick snippet to demonstrate the use of this utility.#…

PowerShell: Get CRC Signature of a File

2/10/20 Update: xxHash will beat both of these in term of speed. Hence, this blog…

PowerShell: Get MD5 Hashing Signature of a File

# Get-MD5-Hash.ps1# Get-FileHash (PowerShell 4.0+) replacement for Windows 2008. Forward-compatible.function getMd5{ param( $file, $md5 =…

PowerShell: Find Process IDs of a Locked File and Kill It

There's a newer iteration of this function here. $targetFile='C:\Users\kimconnect\Desktop\testfile.csv' function killPidLockedFile($filename){ if (!(Get-Command handle.exe -ErrorAction…

PowerShell: Purge User Outlook Profile

# Purge-User-Outlook-Profile.ps1# Set folder path$folderToDelete="$env:localappdata\Microsoft\Outlook";function purgeFolder($path){ mkdir c:\temp -force -ea SilentlyContinue | out-null cd c:\temp…

PowerShell: How To Make A System App Do Nothing

# How-To-Make-Existing-System-App-Do-Nothing.ps1# Provide variables$hive="REGISTRY::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WORDPAD.EXE"$key="(default)"$value="C:\Windows\dummy.exe"$defaultValue="C:\Program Files\Windows NT\Accessories\WORDPAD.EXE"# Dummy-File-Creator.ps1$dummyFile="C:\Windows\dummy.exe"$output = new-object byte[] 1; (new-object Random).NextBytes($output);[IO.File]::WriteAllBytes($dummyFile, $output);if…

PowerShell: Perform Final Sync Between 2 Directories

Current Version # Final-Sync.ps1# This function performs CRC checks on each file at the source.#…