Skip to content

Instantly share code, notes, and snippets.

@SweetAsNZ
Created January 19, 2026 01:30
Show Gist options
  • Select an option

  • Save SweetAsNZ/195cb116076e38846f5635f3aad282f5 to your computer and use it in GitHub Desktop.

Select an option

Save SweetAsNZ/195cb116076e38846f5635f3aad282f5 to your computer and use it in GitHub Desktop.
Multiple Functions to get various Windows system information. Get-DNSServers, Get-DNS, Get-Gateway, Get-GW, Get-IP, Get-MACAddress, Get-MAC, Get-UpTime, Get-DiskSpace, Get-MappedDrives, Test-Gateway, Test-GW, Get-FreeSpace
# Multiple Functions to get various Windows system information
# Get-DNSServers, Get-DNS, Get-Gateway, Get-GW, Get-IP, Get-MACAddress, Get-MAC, Get-UpTime, Get-DiskSpace, Get-MappedDrives, Test-Gateway, Test-GW, Get-FreeSpace
function Get-DNSServers {
<#
.SYNOPSIS
Gets DNS server addresses configured on the system.
.DESCRIPTION
Returns DNS server addresses configured on the system, optionally filtered by interface alias, IPv4 only, and status.
.PARAMETER All
If specified, returns all DNS server addresses with their interface aliases and address families.
.PARAMETER InterfaceAlias
If specified, filters the results to only include DNS servers for the given interface alias.
.PARAMETER IPv4Only
If specified, filters the results to only include IPv4 DNS servers.
.PARAMETER Status
Specifies the status of network adapters to filter by (default is 'Up'). Other possible values
include 'Down', 'Disabled', etc.
.EXAMPLE
Get-DNSServers
.EXAMPLE
Get-DNSServers -IPv4Only -Status Up
.NOTES
Author: Tim West
Company: Air New Zealand
Version: 1.0
Status: Production
Created: 11/11/2025
Updated: 13/11/2025
.CHANGELOG
- Initial creation of the function to retrieve DNS server addresses with filtering options.
#>
[cmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[switch]$All,
[Parameter(Mandatory=$false)]
[string]$InterfaceAlias,
[Parameter(Mandatory=$false)]
[switch]$IPv4Only,
[Parameter(Mandatory=$false)]
[ValidateSet('Up', 'Down', 'Disabled', 'Disconnected', 'NotPresent', 'LowerLayerDown')]
[string]$Status = 'Up'
)
$dnsServers = Get-DnsClientServerAddress | Where-Object {
$_.InterfaceAlias -notlike "Loopback*" -and
$_.InterfaceAlias -notlike "Bluetooth*"
}
# Get enabled adapters only
$enabledAdapters = Get-NetAdapter | Where-Object {$_.Status -eq $Status} | Select-Object -ExpandProperty Name
# Filter to enabled adapters
$dnsServers = $dnsServers | Where-Object {$enabledAdapters -contains $_.InterfaceAlias}
# Apply IPv4 filter if requested
if ($IPv4Only) {
$dnsServers = $dnsServers | Where-Object {$_.AddressFamily -eq 2}
}
# Apply interface filter if specified
if ($InterfaceAlias) {
$dnsServers = $dnsServers | Where-Object {$_.InterfaceAlias -eq $InterfaceAlias}
}
if ($All) {
return ($dnsServers | Select-Object InterfaceAlias, AddressFamily, ServerAddresses | Sort-Object InterfaceAlias)
}
else {
return ($dnsServers | Select-Object -ExpandProperty ServerAddresses | Sort-Object -Unique)
}
}
function Get-DNS{
# Alias Function Get-DNS to Get-DNSServers coz I'm/You're lazy ;-)
Get-DNSServers @PSBoundParameters
}
function Get-Gateway {
<#
.SYNOPSIS
Gets the default gateway address.
.DESCRIPTION
Returns the default gateway address used for routing traffic to external networks.
.EXAMPLE
Get-Gateway
.NOTES
Author: Tim West
Company: Air New Zealand
Version: 1.0
Status: Production
Created: 11/11/2025
Updated: 13/11/2025
#>
[CmdletBinding()]
param()
return (Get-NetRoute -DestinationPrefix "0.0.0.0/0").NextHop
}
function Get-GW {
# Alias Function for the lazy/disallusioned among us ;-)
Get-Gateway @PSBoundParameters
}
function Get-IP {
<#
.SYNOPSIS
Gets IP addresses configured on the system.
.DESCRIPTION
Returns IP addresses configured on the system, optionally filtered by interface alias, IPv6 addresses,
and address state.
.PARAMETER InterfaceAlias
If specified, filters the results to only include IP addresses for the given interface alias.
.PARAMETER IpV6Too
If specified, includes IPv6 addresses in the results.
.PARAMETER All
If specified, returns all IP addresses with their interface aliases and address families.
.EXAMPLE
Get-IP
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 13/11/2025
Updated: 13/11/2025
Status: Production
Version: 1.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string]$InterfaceAlias,
[Parameter(Mandatory=$false)]
[switch]$IpV6Too,
[Parameter(Mandatory=$false)]
[switch]$All
)
if($All){
return (Get-NetIPAddress | Select-Object InterfaceAlias,AddressFamily,IPAddress,PrefixOrigin,AddressState | FT -AutoSize)
}
elseif($IpV6Too){
return (Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -or $_.AddressFamily -eq 'IPv6' -and $_.IPAddress -ne '127.0.0.1' -and $_.IPAddress -ne '::1' -and $_.AddressState -eq 'Preferred'} |
Select-Object InterfaceAlias,IPAddress,PrefixOrigin,AddressState | FT -AutoSize)
}
elseif($InterfaceAlias){
return (Get-NetIPAddress -InterfaceAlias $InterfaceAlias | Select-Object InterfaceAlias,IPAddress,PrefixOrigin,AddressState | FT -AutoSize)
}
else{
return ((Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.IPAddress -ne '127.0.0.1' -and $_.AddressState -eq 'Preferred'} |
Select-Object InterfaceAlias,IPAddress,PrefixOrigin,AddressState).IPAddress)
}
}
function Get-MACAddress {
<#
.SYNOPSIS
Gets MAC addresses for network adapters.
.DESCRIPTION
Returns MAC addresses for network adapters on the system. By default, returns only MAC addresses
for adapters with 'Up' status. Use -All switch to return all adapters with their names and status.
.PARAMETER All
If specified, returns all network adapters with their names, MAC addresses, and status.
.PARAMETER Status
Specifies the status of network adapters to filter by (default is 'Up'). Possible values include
'Up', 'Down', 'Disabled', 'Disconnected', 'NotPresent', or 'LowerLayerDown'.
.EXAMPLE
Get-MACAddress
.EXAMPLE
Get-MACAddress -All
.EXAMPLE
Get-MACAddress -Status Down
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 11/11/2025
Updated: 13/11/2025
Status: Production
Version: 1.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[switch]$All,
[Parameter(Mandatory=$false)]
[ValidateSet('Up', 'Down', 'Disabled', 'Disconnected', 'NotPresent', 'LowerLayerDown')]
[string]$Status = "Up"
)
if($All){
return (Get-NetAdapter | Select Name, MacAddress, Status)
}
else{
return (Get-NetAdapter | Where-Object {$_.Status -eq $Status} | Select Name, MacAddress, Status).MacAddress
}
}
function Get-MAC {
# Alias Function Get-MAC to Get-MACAddress coz my/your fingers hurt from all the typing ;-)
Get-MACAddress @PSBoundParameters
}
function Get-UpTime {
<#
.SYNOPSIS
Gets system uptime in formatted string.
.DESCRIPTION
Returns the system uptime since last boot in days.hours:minutes:seconds format.
.EXAMPLE
Get-UpTime
Returns something like "5.12:34:56" for 5 days, 12 hours, 34 minutes, 56 seconds uptime.
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 13/11/2025
Updated: 13/11/2025
Status: Production
Version: 1.0
#>
$lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime ; $lastBootTime
$uptimeSpan = New-TimeSpan -Start $lastBootTime -End (Get-Date)
Write-Host -f Green "Days.Hours:Mins:Secs"
return $uptimeSpan.ToString("d\.hh\:mm\:ss")
}
function Get-DiskSpace {
<#
.SYNOPSIS
Gets disk space information for all drives, doesn't look at mount points though
.DESCRIPTION
Returns disk space information for all drives, including free space and total space in GB.
.PARAMETER FreeSpace
If specified, only returns free space for each drive.
.EXAMPLE
Get-DiskSpace
.EXAMPLE
Get-DiskSpace -FreeSpaceOnly -Drives C,X
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 13/11/2025
Updated: 13/11/2025
Status: Production
Version: 1.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[switch]$FreeSpaceOnly,
[Parameter(Mandatory=$false)]
[ValidateSet('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')]
[string[]]$Drives
)
if ($Drives) {
$drivesToProcess = Get-PSDrive -PSProvider FileSystem | Where-Object { $Drives -contains $_.Name }
} else {
$drivesToProcess = Get-PSDrive -PSProvider FileSystem
}
if ($drivesToProcess.Count -eq 0) {
Write-Host "No drives found." -ForegroundColor Yellow
return
}
$drivesToProcess | ForEach-Object {
$drive = $_
$freeSpaceGB = [math]::Round($drive.Free / 1GB, 2)
$totalSpaceGB = [math]::Round(($drive.Used + $drive.Free) / 1GB, 2)
if ($freeSpaceGB -lt 5) {
if($FreeSpaceOnly){
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB" -ForegroundColor Red
}
else{
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB, Total Space: $totalSpaceGB GB" -ForegroundColor Red
}
}
elseif ($freeSpaceGB -lt 10) {
if($FreeSpaceOnly){
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB" -ForegroundColor Yellow
}
else {
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB, Total Space: $totalSpaceGB GB" -ForegroundColor Yellow
}
}
else {
if($FreeSpaceOnly){
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB" -ForegroundColor Green
}
else {
Write-Host "Drive: $($drive.Name) - Free Space: $freeSpaceGB GB, Total Space: $totalSpaceGB GB" -ForegroundColor Green
}
}
}
}
function Get-MappedDrives {
<#
.SYNOPSIS
Gets mapped network drives.
.DESCRIPTION
Returns a list of mapped network drives with their names and paths.
.PARAMETER UseWMI
If specified, uses WMI to retrieve mapped drives instead of PowerShell drives.
.EXAMPLE
Get-MappedDrives
.EXAMPLE
Get-MappedDrives -UseWMI
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 13/11/2025
Status: Production
Version: 1.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[switch]$UseWMI
)
if($UseWMI){
$mappedDrives = Get-WmiObject -Class Win32_NetworkConnection | Select-Object Name, RemoteName
if ($mappedDrives) {
$mappedDrives | ForEach-Object {
return "Drive: $($_.Name) - Path: $($_.RemoteName)"
}
} else {
Write-Host "No mapped drives found."
}
} else {
$mappedDrives = Get-PSDrive -PSProvider FileSystem | Where-Object {
$_.DisplayRoot -like "\\*"
} | Select-Object Name, DisplayRoot
if ($mappedDrives) {
$mappedDrives | ForEach-Object {
return "$($_.Name): $($_.DisplayRoot)"
}
} else {
Write-Host "No mapped drives found."
}
}
}
function Test-Gateway {
<#
.SYNOPSIS
Tests connectivity to the default gateway.
.DESCRIPTION
Pings the default gateway to check if it is reachable.
.EXAMPLE
Test-Gateway
.NOTES
Author: Tim West
Company: Air New Zealand
Created: 13/11/2025
Updated: 13/11/2025
Status: Production
Version: 1.0
.CHANGELOG
- Initial creation of the function to test connectivity to the default gateway.
#>
[CmdletBinding()]
param()
$gateway = Get-Gateway
if ($gateway) {
Write-Host "Pinging default gateway: $gateway"
$pingResult = Test-Connection -ComputerName $gateway -Count 1 -ErrorAction SilentlyContinue
if ($pingResult) {
Write-Host "Gateway is reachable." -ForegroundColor Green
} else {
Write-Host "Gateway is not reachable." -ForegroundColor Red
}
} else {
Write-Host "No default gateway found." -ForegroundColor Yellow
}
}
function Test-GW {
# Alias Function Test-GW to Test-Gateway coz my/your fingers hurt from all the typing plus one of us is lazy ;-)
Test-Gateway @PSBoundParameters
}
function Get-FreeSpace {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
[string]$DriveLetter
)
$ENV:COMPUTERNAME
if($DriveLetter){
Get-PSDrive | Where-Object { $_.Provider -like "*FileSystem" -and $_.Name -eq $DriveLetter } | Select-Object Name,
@{Name="Free(GB)"; Expression = { [math]::Round(($_.Free / 1GB), 1) }},
@{Name="Used(GB)"; Expression = { [math]::Round(($_.Used / 1GB), 1) }}, Root
}
else{
Get-PSDrive | Where-Object { $_.Provider -like "*FileSystem" } | Select-Object Name,
@{Name="Free(GB)"; Expression = { [math]::Round(($_.Free / 1GB), 1) }},
@{Name="Used(GB)"; Expression = { [math]::Round(($_.Used / 1GB), 1) }}, Root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment