Skip to content

Instantly share code, notes, and snippets.

View Goadstir's full-sized avatar
🎓
Always Learning

Brandon Goad Goadstir

🎓
Always Learning
View GitHub Profile
@Goadstir
Goadstir / Get-DateFormatted.ps1
Created January 8, 2020 21:18
PowerShell: Example Get-Date formatting
Get-Date -Format FileDate # 20180502
Get-Date -Format FileDateUniversal # 20180502Z
Get-Date -Format FileDateTime # 20180502T1852552818
Get-Date -Format FileDateTimeUniversal # 20180502T2255061745Z
Get-Date -Format f # Wednesday, May 02, 2018 6:51 PM
Get-Date -Format F # Wednesday, May 02, 2018 6:51:03 PM
Get-Date -Format g # 5/2/2018 6:51 PM
Get-Date -Format G # 5/2/2018 6:51:03 PM
Get-Date -Format s # 2018-05-02T18:51:03
@Goadstir
Goadstir / Get-CheckRequiredModule.ps1
Created January 8, 2020 21:17
PowerShell: Ensure a PowerShell Module is available for Script or Function
#####
# Require Module as part of a Script
#####
# This examples checks for available PowerCLI module: VMware.VimAutomation.Core
# Add "#REQUIRES" to the top of your .ps1 file
# Use -Modules to specify a Module name
# If the required modules are not in the current session, Windows PowerShell imports them.
# If the modules cannot be imported, Windows PowerShell throws a terminating error.
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-6
@Goadstir
Goadstir / Remove-CharFromString.ps1
Created January 8, 2020 21:16
PowerShell: Remove the curly braces surrounding a string, or any character from within a string using an option within these examples.
# Remove the Curly Braces from a string such as after getting PSChildName. E.g. {636D9115-E54E-4673-B992-B51A8F8DDC8B}
# Using trim :
"{636D9115-E54E-4673-B992-B51A8F8DDC8B}".trim('{}')
# Using multiple replace :
"{636D9115-E54E-4673-B992-B51A8F8DDC8B}".Replace('{','').Replace('}','')
# Using Replace with RegEx
"{636D9115-E54E-4673-B992-B51A8F8DDC8B}" -replace '\{(.*)\}','$1'
@Goadstir
Goadstir / Get-OsType.ps1
Created January 8, 2020 20:43
PowerShell: Check if endpoint is a Desktop or Server OS
$OS = ((Get-WmiObject Win32_OperatingSystem).Name).Split("|")[0]
# Checking for Windows 7 or Windows 10 in Name
If ($OS -contains "7" -or "10") {Write-Host "It's a Desktop!"}
Else {Write-Host "It's a Server!"}
@Goadstir
Goadstir / Get-LastRebootTime.ps1
Created January 8, 2020 20:42
PowerShell: Get the last reboot of the host
# find out last boot time
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$lastBoot = $os.LastBootUpTime
# raw datetime output
$lastBoot
# e.g. \> Wednesday, December 5, 2018 2:31:48 AM
# formatted string output
Get-Date -Date $lastBoot -Format '"Last reboot at" MMM dd, yyyy "at" HH:mm:ss "and" fffff "Milliseconds."'
@Goadstir
Goadstir / New-TimerStopWatch.ps1
Last active April 17, 2024 08:18
PowerShell: Using Timer to track time elapsed
$timeout = New-TimeSpan -Seconds 30
$timer = [Diagnostics.Stopwatch]::new() # Create a timer
$timerAndStart = [Diagnostics.Stopwatch]::StartNew() # Create a timer and start it
$timer.Start() # Start the timer
$timer.Stop() # Stop the timer
$timer.Elapsed # Get time elapsed
$timer.IsRunning # Is timer running?
$timer.Restart() # restarts the time, doesn't stop
$timer.Reset() # stops timer and sets to 0
$timer.Equals($timeout) # check if timer value equals a value such as timeout, returns boolean
@Goadstir
Goadstir / New-OutputFormatted.ps1
Created January 8, 2020 20:39
PowerShell: Create Nicely Formatted Output
<#
References:
* http://powershell-guru.com/dont-13-use-write-host-properly-align-output/
#>
# Solution 1
[PSCustomObject] @{
Date = Get-Date -Format d
Computer = [System.Environment]::MachineName
Username = ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
@Goadstir
Goadstir / Wait-ForProcessFinishThenContinue.ps1
Created January 8, 2020 20:33
PowerShell: Wait for a process to finish and then continue
<#
Resources:
* https://blogs.technet.microsoft.com/dsheehan/2018/02/18/powershell-code-to-wait-for-a-background-process-to-finish-before-continuing-on-with-a-script/
#>
# Check to see if the .NET Optimization Service is still running as it can block the Exchange initialization process.
If (Get-Process mscorsvw -ErrorAction SilentlyContinue) {
# It is so define the number of minutes the countdown timer should allow it to gracefully finish before giving up and exiting the script.
$CountDownTimer = 10
Write-Host " "
Write-Warning "The .NET Optimization Service is still running. Waiting up to $CountDownTimer minutes for it to gracefully shut down so the script can proceed."
@Goadstir
Goadstir / Get-InstalledApplications.ps1
Created January 8, 2020 20:26
PowerShell: Get List of Installed Applications
# Get the installed software from your Windows Operating Systems
# From the WMI Object perspective
Get-WmiObject -Class Win32_Product | Sort-Object Name | Select-Object Name,Vendor,Version,Caption | Format-Table -AutoSize
# From the registry perspective
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Sort-Object DisplayName | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize
@Goadstir
Goadstir / New-CredentialObject.ps1
Created January 8, 2020 17:29
PowerShell: Create and View Credential Objects
# create secure string from plain-text string
$secureString = ConvertTo-SecureString -AsPlainText -Force -String $password
Write-Host "Secure string:",$secureString
# convert secure string to encrypted string (for safe-ish storage to config/file/etc.)
$encryptedString = ConvertFrom-SecureString -SecureString $secureString
Write-Host "Encrypted string:",$encryptedString
# convert encrypted string back to secure string
$secureString = ConvertTo-SecureString -String $encryptedString