Skip to content

Instantly share code, notes, and snippets.

@matheuseduardo
Created February 19, 2025 03:22
Show Gist options
  • Save matheuseduardo/9120312004b240f12eee36cebe07967a to your computer and use it in GitHub Desktop.
Save matheuseduardo/9120312004b240f12eee36cebe07967a to your computer and use it in GitHub Desktop.
prompt do powershell no windows
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
}
function Get-ExecutionTime {
$LastCommand = Get-History -Count 1
if ($LastCommand) {
return $LastCommand.EndExecutionTime - $LastCommand.StartExecutionTime;
}
return [TimeSpan]::Zero;
}
enum OS
{
Unknown = 0
Windows = 1
Linux = 2
MacOS = 3
}
# I map these booleans to an Enum so that I can switch over them
$global:OperatingSystem = if ([OperatingSystem]::IsWindows()) {
[OS]::Windows
} elseif ([OperatingSystem]::IsLinux()) {
[OS]::Linux
} elseif ([OperatingSystem]::IsMacOS()) {
[OS]::MacOS
} else {
[OS]::Unknown
}
# In the prompt function below this is used to toggle the leading char ('>' or '#') to indicate elevated privilages
if ([OperatingSystem]::IsWindows()) {
$global:IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if ([OperatingSystem]::IsLinux()) {
$global:IsAdmin = $(id -u) -eq 0
}
if ([OperatingSystem]::IsMacOS()) {
$global:IsAdmin = [bool](sudo -n true 2>&1)
}
function prompt {
$promptParts = [System.Collections.ArrayList]@()
# Data/hora
$promptParts.AddRange(@(
"$($PSStyle.Foreground.DarkGray)[$(Get-Date -Format 'dd/MM @ HH:mm:ss')]$($PSStyle.Reset)"
))
# Usuário e hostname
$promptParts.AddRange(@(
" $($PSStyle.Foreground.Green)$env:USERNAME",
"$($PSStyle.Foreground.White) @ ",
"$($PSStyle.Foreground.Cyan)$($env:COMPUTERNAME ?? $(hostname))$($PSStyle.Reset)"
))
# Diretório atual
$promptParts.AddRange(@(
" $($PSStyle.Foreground.DarkYellow)$(Get-Location)$($PSStyle.Reset)"
))
# Tempo de execução (se > 0)
$execTime = Get-ExecutionTime
if ($execTime.Ticks -gt 0) {
$promptParts.AddRange(@(
" $($PSStyle.Foreground.Yellow)($($execTime.ToString('hh\:mm\:ss\.fff')))$($PSStyle.Reset)"
))
}
# Branch do Git
if ($branch = git branch --show-current 2>$null) {
$promptParts.AddRange(@(
" $($PSStyle.Foreground.Blue)[$branch]$($PSStyle.Reset)"
))
}
# Linha adicional com o caractere do prompt
$promptParts.AddRange(@(
"`n$($global:IsAdmin ? '#' : '>') "
))
Write-Host ($promptParts -join '') -NoNewline
return " "
}
. "$env:USERPROFILE\Documents\PowerShell\Scripts\GetDiskInfo.ps1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment