Skip to content

Instantly share code, notes, and snippets.

@henno
Last active January 12, 2024 07:49
Show Gist options
  • Select an option

  • Save henno/4bdd84b317616edce0b70ae7b30ba339 to your computer and use it in GitHub Desktop.

Select an option

Save henno/4bdd84b317616edce0b70ae7b30ba339 to your computer and use it in GitHub Desktop.
ll for PowerShell
# First type notepad $profile in powershell
# Then paste the following code in the notepad and save it
# Then restart the powershell or type . $profile in powershell
# Then type ll in powershell to see the linux style directory listing
function Get-LinuxStyleChildItem {
Get-ChildItem -Force | ForEach-Object {
$item = $_
$size = if ($item.Length -ge 1GB) {
"{0:N2} GB" -f ($item.Length / 1GB)
} elseif ($item.Length -ge 1MB) {
"{0:N2} MB" -f ($item.Length / 1MB)
} elseif ($item.Length -ge 1KB) {
"{0:N2} KB" -f ($item.Length / 1KB)
} else {
"$($item.Length) B"
}
$mode = if ($item.PSIsContainer) { "d" } else { "-" }
$mode += if ($item.Attributes -match 'ReadOnly') { "r" } else { "-" }
$mode += if ($item.Attributes -match 'Hidden') { "h" } else { "-" }
$mode += if ($item.Attributes -match 'System') { "s" } else { "-" }
$mode += if ($item.Attributes -match 'Archive') { "a" } else { "-" }
$lastWriteTime = $item.LastWriteTime.ToString("dd.MM.yyyy HH:mm:ss")
# Determine the color
$color = "Gray" # Default color for files
if ($item.PSIsContainer) {
$color = "Blue" # Color for directories
} elseif ($item.LinkType) {
$color = "Cyan" # Color for links
}
# Write the line with color
Write-Host ("{0,10} {1,10} {2,20} {3,-20}" -f $mode, $size, $lastWriteTime, $item.Name) -ForegroundColor $color
}
}
Set-Alias -Name ll -Value Get-LinuxStyleChildItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment