Skip to content

Instantly share code, notes, and snippets.

@mambroziak
Created September 16, 2021 03:35
Show Gist options
  • Save mambroziak/073f748543d203a7e427540c06091670 to your computer and use it in GitHub Desktop.
Save mambroziak/073f748543d203a7e427540c06091670 to your computer and use it in GitHub Desktop.
PowerShell Time Zone Picker
clear
$tz_found = $false
do {
$q = "`nEnter the first 3 letters of time zone you want to set (e.g. Eastern = eas)"
do {
$tz_query = Read-Host -Prompt $q
} until ($tz_query.Length -gt 2)
$timezone_list = Get-TimeZone -Name "*$($tz_query)*" -ErrorAction SilentlyContinue
if ($timezone_list.Length -gt 0) {
Write-Host "`nThe following time zones were found:`n"
foreach ($tz in $timezone_list) { Write-Host "$([array]::indexof($timezone_list,$tz))) $($tz.DisplayName)" }
do {
$tz_in_list = Read-Host -Prompt "`nDid you find your time zone in the list? [yes/no]"
if ($tz_in_list.ToLower() -eq "yes") {
$tz_found = $true
Write-Host "OK" -ForegroundColor Green
}
elseif ($tz_in_list.ToLower() -in @("no","n")) {
Write-Host "Bummer. Let's try again.`n" -ForegroundColor Yellow
}
} until ($tz_in_list.ToLower() -in @("yes","no","n"))
}
else { Write-Host "No time zones found. Please try again." -ForegroundColor Red }
} until ($tz_found -eq $true)
$q1 = "`nEnter the item number from the first column of the time zone list. [0-$($timezone_list.Length-1)]"
do {
$tz_query_index = Read-Host -Prompt $q1
if ($tz_query_index -match "^\d+$" -and $timezone_list[$tz_query_index]) {
Write-Host "OK" -ForegroundColor Green
$tz_query_index = [int]$tz_query_index
Write-Host
$q2 = "You selected '$($timezone_list[$tz_query_index])'. Proceed to set the time zone? [yes/no]"
do {
$tz_sel_correct = Read-Host -Prompt $q2
} until ($tz_sel_correct.ToLower() -in @("yes","no","n"))
if ($tz_sel_correct -eq "yes") {
Write-Host "OK" -ForegroundColor Green
Set-TimeZone –Name $timezone_list[$tz_query_index].StandardName
Write-Host "`nThe time zone has been set." -ForegroundColor Green
}
else {
$tz_query_index = $null # Force another loop
}
}
elseif ($tz_query_index -match "^\d+$" -and !$timezone_list[$tz_query_index]) {
Write-Host "The item number you entered was not found in the time zone list. Please try again." -ForegroundColor Red
}
else {
Write-Host "Please enter a numerical value." -ForegroundColor Red
}
} until ($tz_query_index -match "^\d+$" -and $timezone_list[$tz_query_index]) # Verify the index is a number and the index exists in the array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment