Skip to content

Instantly share code, notes, and snippets.

@matheuseduardo
Created February 18, 2025 23:41
Show Gist options
  • Save matheuseduardo/5cafa0950a02ae6b1d7b1de7dadaecf3 to your computer and use it in GitHub Desktop.
Save matheuseduardo/5cafa0950a02ae6b1d7b1de7dadaecf3 to your computer and use it in GitHub Desktop.
função para retornar informações dos discos, com opção de filtrar/selecionar resultados
function Get-DiskInfo {
<#
.SYNOPSIS
Obtém informações detalhadas sobre discos e volumes com opções de filtro
.PARAMETER Letter
Filtra por letra(s) de unidade específica(s) (ex: "C", "D,E,F")
.PARAMETER Name
Filtra discos pelo nome/modelo contendo o texto especificado (case-insensitive)
.EXAMPLE
Get-DiskInfo
Lista todas as unidades e discos
.EXAMPLE
Get-DiskInfo -Letter "C" -Name "SSD"
Lista apenas a unidade C em discos que contenham "SSD" no nome
#>
param(
[Parameter(Position = 0)]
[string[]]$Letter,
[Parameter(Position = 1)]
[string]$Name
)
$allDisks = Get-Disk | ForEach-Object {
$disk = $_
$partitions = $disk | Get-Partition -ErrorAction SilentlyContinue
foreach ($partition in $partitions) {
$volume = $partition | Get-Volume -ErrorAction SilentlyContinue
if ($volume) {
[PSCustomObject]@{
DiskNumber = $disk.Number
DiskName = $disk.FriendlyName
DiskSizeGB = [math]::Round($disk.Size / 1GB, 2)
DiskSerial = $disk.SerialNumber.Trim()
PartitionType = $partition.Type
DriveLetter = if ($volume.DriveLetter) { $volume.DriveLetter } else { "N/A" }
VolumeSizeGB = [math]::Round($volume.Size / 1GB, 2)
UsedSpaceGB = [math]::Round(($volume.Size - $volume.SizeRemaining) / 1GB, 2)
FreeSpaceGB = [math]::Round($volume.SizeRemaining / 1GB, 2)
FileSystem = $volume.FileSystem
}
}
}
}
# Aplicar filtros
$filtered = $allDisks
if ($Letter) {
$filtered = $filtered | Where-Object { $Letter -contains $_.DriveLetter }
}
if ($Name) {
$filtered = $filtered | Where-Object { $_.DiskName -match [regex]::Escape($Name) -like "*$Name*" } |
Where-Object { $_.DiskName -match $Name -or $_.DiskName -like "*$Name*" }
}
$filtered | Sort-Object DiskNumber, DriveLetter | Format-Table -AutoSize
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment