Last active
December 9, 2024 07:28
-
-
Save AfroThundr3007730/8d149e7066cd29196e0c7b965cfb912b to your computer and use it in GitHub Desktop.
Collection of useful powershell snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Usage filesplit.ps1 <filename> <# of lines> | |
Param( | |
[parameter(mandatory=$true,position=0)][string]$filename, | |
[parameter(position=1)][string]$linesperFile = 100000 | |
) | |
$sw = new-object System.Diagnostics.Stopwatch | |
$sw.Start() | |
$rootName = (join-path ([io.fileinfo]$filename).directoryname ([io.fileinfo]$filename).basename) + '_' | |
$ext = ([io.fileinfo]$filename).extension | |
$header = get-content $filename -first 1 | |
$filecount = 0 | |
$reader = $null | |
try { | |
$reader = [io.file]::OpenText($filename) | |
try { | |
$filecount++ | |
$linecount = 0 | |
"Creating file number $filecount" | |
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext)) | |
while($reader.EndOfStream -ne $true) { | |
"Reading $linesperFile lines" | |
if($filecount -gt 1){$writer.WriteLine($header)} | |
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)) { | |
$writer.WriteLine($reader.ReadLine()); | |
$linecount++ | |
} | |
if($reader.EndOfStream -ne $true) { | |
"Closing file" | |
$writer.Dispose(); | |
$filecount++ | |
$linecount = 0 | |
"Creating file number $filecount" | |
$writer = [io.file]::CreateText("{0}{1}{2}" -f ($rootName,$filecount.ToString("000"),$ext)) | |
} | |
} | |
} finally { | |
$writer.Dispose(); | |
} | |
} finally { | |
$reader.Dispose(); | |
} | |
$sw.Stop() | |
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sends PrintScreen every 60 seconds | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") | |
While ($true) { | |
[System.Windows.Forms.SendKeys]::SendWait("{PRTSC}") | |
Start-Sleep -s 30 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Used to publish new user photo to GAL | |
param ( | |
[string]$sid = $(throw "-sid is required"), | |
[string]$photo = $(throw "-photo is required") | |
) | |
$ErrorActionPreference = "stop" | |
$root = [adsi]'' | |
$searcher = New-Object System.DirectoryServices.DirectorySearcher($root) | |
$searcher.Filter = "(mail=$($sid)*)" | |
$users = $searcher.FindAll() | |
if($users.Count -eq 0){ | |
throw "No matching user found" | |
} elseif($users.Count -eq 1){ | |
$user = $users[0].GetDirectoryEntry() | |
} else{ | |
$i = 1 | |
foreach($user in $users){ | |
Write-Host "$i : $($user.Path)" | |
$i += 1 | |
} | |
$pick = Read-Host "Which user?" | |
$user = $users[$pick - 1].GetDirectoryEntry() | |
} | |
$file = [byte[]](Get-Content $photo -Encoding Byte) | |
$user.Properties["thumbnailPhoto"].Clear() | |
$user.Properties["thumbnailPhoto"].Add($file) | |
$user.commitChanges() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment