Skip to content

Instantly share code, notes, and snippets.

View dpo007's full-sized avatar

Dev dpo007

  • Ontario, Canada
View GitHub Profile
@dpo007
dpo007 / GetHostNameFromUNC.ps1
Created April 13, 2020 19:31
PowerShell Function :: Get HostName From UNC Path
function GetHostNameFromUNC {
param (
[Parameter(Mandatory=$true)]
[string]$UNCPath
)
$UNCPath -match '\\\\(.*?)\\' | Out-Null
if ($Matches.Count -ge 2) {
return $Matches[1]
} else {
@dpo007
dpo007 / Get-CatFact.ps1
Last active November 2, 2023 03:30
PowerShell :: The Best Login Script Ever
# Get random cat fact from API
$catFact = Invoke-RestMethod -Uri "https://catfact.ninja/fact" -Method Get
# Print cat fact
Write-Host $catFact.fact
# Setup speech synthesizer
Add-Type -AssemblyName System.Speech
$speech = New-Object System.Speech.Synthesis.SpeechSynthesizer
@dpo007
dpo007 / GetPickListChoice.ps1
Created September 14, 2020 21:46
PowerShell :: Console input with search list by typed letters, tab cycling, etc.
function GetPickListChoice {
param (
[Parameter(Mandatory=$true)]
[string[]]$PickList,
[string]$Prompt = 'Type to search, Tab to cycle matches, Backspace to reset, ''?'' for a list: ',
[switch]$PromptNoNewLine
)
function ClearCurrentChoice {
for ($i = $origCursorPos.X; $i -lt $endPos; $i++) {
@dpo007
dpo007 / UploadToS3Bucket.ps1
Created October 23, 2020 17:18
PowerShell :: Upload folder of files to S3 bucket
Install-Module -Name AWS.Tools.Installer
Install-AWSToolsModule AWS.Tools.S3 -CleanUp
$bucket = 'some-s3-bucket'
Set-AWSCredential `
-AccessKey '<Get Your Own>' `
-SecretKey '<Get Your Own>'
# Gather files to upload
@dpo007
dpo007 / MoveFilesBetweenTeams.ps1
Last active June 29, 2023 19:05
PowerShell :: Move all folders between Teams
#Requires -Version 5.0
#Requires -Modules SharePointPnPPowerShellOnline
<#
.SYNOPSIS
Script to move folders and files from a source team's site to a destination team's site in SharePoint Online.
.DESCRIPTION
This PowerShell script requires PowerShell version 5.0 and the SharePointPnPPowerShellOnline module. It connects to SharePoint Online and retrieves the source team's site and the destination team's site based on the provided team names. It then retrieves all folders and files in the source team's site and moves each item to the destination team's site.
.PARAMETER SrcTeam
@dpo007
dpo007 / Get-UsersWithMismatchedHomeDirectory.ps1
Last active June 29, 2023 19:02
PowerShell :: Get-UsersWithMismatchedHomeDirectory
<#
.SYNOPSIS
Retrieves users whose home directory paths do not contain their SamAccountName.
.DESCRIPTION
The Get-UsersWithMismatchedHomeDirectory function retrieves users from Active Directory whose home directory paths do not contain their SamAccountName. By default, it excludes users with empty home directories. Users can be optionally included by specifying the -IncludeEmptyHomeDirs switch parameter.
.PARAMETER IncludeEmptyHomeDirs
Switch parameter to include users with empty home directories in the results.
@dpo007
dpo007 / Find-NonMatchingADUserFolders.ps1
Created June 29, 2023 18:59
PowerShell :: Find-NonMatchingADUserFolders
<#
.SYNOPSIS
Script to find subfolders without matching Active Directory (AD) users and optionally move them to a destination folder.
.DESCRIPTION
This PowerShell script imports the Active Directory module and defines a function named Find-NonMatchingADUserFolders. The function takes two parameters: folderPath (mandatory) and destinationFolder (optional). It retrieves all subfolders in the specified folder and all existing AD users. It then iterates through each subfolder, checks if the subfolder name matches an existing AD user, and performs the specified action (move or list) based on the presence or absence of a matching user.
.EXAMPLE
Find-NonMatchingADUserFolders -folderPath "C:\Path\To\Folder" -destinationFolder "D:\Temp\OldUserFolders"
Moves the subfolders without matching AD users to the specified destination folder.
@dpo007
dpo007 / Detect-PortableComputer.ps1
Created August 29, 2022 14:46
PowerShell :: Detect if local computer is portable or not, based on WMI/ChassisType.
$isPortable = $false
$chassisType = (Get-CimInstance -Query "Select * from Win32_SystemEnclosure").ChassisTypes[0]
switch ($chassisType) {
# Case 8 "Portable"
# Case 9 "Laptop"
# Case 10 "Notebook"
# Case 11 "Handheld"
# Case 14 "Sub-Notebook"
{$_ -in 8, 9, 10, 11, 14} { $isPortable = $true }
@dpo007
dpo007 / RunAtTime.ps1
Last active August 29, 2022 05:20
PowerShell :: Simple loop to run commands at a certain time in the future.
$endTime = Get-Date "4:45PM"
Do {
$timeNow = Get-Date
if ($timeNow -ge $endTime) {
Write-Host "Ding! It's $endTime, time to work..."
# Do Stuff
Break
} else {
cls
Write-Host "Waiting until $endTime (Currently: $timeNow)"
@dpo007
dpo007 / HyperVReplicationSetup.ps1
Created August 29, 2022 05:17
PowerShell :: Configure Hyper-V replication between a set of host servers.
<#
Configures Hyper-V replication between a set of host servers.
v1.0 - DPO - Aug. 2022
#>
#Requires -RunAsAdministrator
#Requires -Version 5.1
param (
[string]$HostReplicaFolderPath = 'D:\ReplicaVMs',
[switch]$LeaveExistingAllowedServers
)