Category: Codes

PowerShell: Change Process Priority Level

Most programs would launch with normal priories and trigger child processes with varying priority levels,…

PowerShell: Check TCP Connections of Server by Port Numbers

# Check-TCP-Connections.ps1# This function will output progress onto the console as well as returning a…

PowerShell: Stop Any Service on Windows!

Problem: some system protected services cannot be stopped. PS C:\Users\KingKong> stop-service msmpsvcStop-Service : Service 'Microsoft…

PowerShell: Implementation of Anonymous Self Executing Function

JavaScript has this. It's a little known secret that PowerShell can do this as well.…

PowerShell: Copy SMB Share Permissions from Legacy Sources

Scenario:Windows 2008 File Servers migration lacks a built-in function to clone Share Permissions - Get-SmbShareAccess…

PowerShell: Install DotNet 3.5 – the Easy Way

$package="dotnet3.5"# Install Chocolateyif (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}#…

PowerShell: Get Hyper-V Host Name from Inside Guest VM

$guestVMName="SOMENAME"function getHyperVHostname{ param([string]$guestVMName=$env:computername) $hive = [Microsoft.Win32.RegistryHive]::LocalMachine; $keyPath = 'SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters'; $value = 'HostName'; $reg =…

PowerShell: Search for Failed Logins on Primary Domain Controller

# Quick Script to search for failed logins$daysLimit=7$userName="Bruce"$todaysDate= Get-date$pdc = (Get-ADDomain).PDCEmulator #$allDCs = ((Get-ADForest).Domains |…

PowerShell: Check if a HostName is Resolvable on All Internal DNS Servers

# Check if servername is resolvable at all DCs$serverName="MIGRATED-SHERVER" function checkDns{ param( $serverName, $dnsServers=(Get-ADDomainController -Filter…

PowerShell: Delete Hidden System Volume Information Directory

# Incident:The System Volume Information has been copied over to an SMB share, where such…

PowerShell: Comparing 2 Directories

# Set source and destination$source="C:\temp"$destination="C:\tempcopy"# Mirror the 2 directoriesrobocopy $source $destination /MIR /R:0 /NP# create…

PowerShell: Create and Delete VSS Snapshots

Update 7/30/20: use this newer version # There are 2 functions in this snippet# 1.…

PowerShell: Get ‘Size on Disk’ of Files in Windows

There are many methods of attempting to obtain "size on disk" values of files in…

PowerShell: Start-Job, Get-Job, Receive-Job Examples

Demo 1: Obtain Public IP of a Windows Machine # Commands to run locally$command1={(Invoke-WebRequest -URI…

PowerShell: Nesting Functions Inside Functions

Demo 1: calling a function from within another function function A{ Param($functionToCall) Write-Host "Calling function:…

PowerShell: Working With Arrays

Problem: elements of a System.Array couldn't be counted # Example output when an object is…

PowerShell: Obtain Computer Account Parent Container from Invoke-Command

# ObtainComputerAccountParentContainer.ps1# This is a demonstration of how to pass function into an invoke-command as…

PowerShell: Snippet to Detect and Disconnect Active PS Sessions

# Manual DetectionPS C:\Windows\system32> get-pssession Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------…

PowerShell: Compare Time Stamps of Items in Mirroring Directories

# compareTimeStamps.ps1# Set variables$source="T:\DIRECTORY1"$destination="\\FILESHERVER\DIRECTORY1"$sampleSize=100$logFile="\\FILESHERVER\SHERVER1\sampleTimeStampLog.txt"function sampleTimeStamp{ param( [string]$sourceDirectory=$source, [string]$destinationDirectory=$destination, [int]$sampleCount=$sampleSize, [string]$timeStampLog=$logFile ) $timeStampTimer=[System.Diagnostics.Stopwatch]::StartNew() # Enable Remote…

Resolve Windows 2003 Logon Screen Blackout Problems

The problem:Administrators are unable to logon to a Windows 2003 Server. This is what they…