475 views
PowerShellでlsもどきを作る === Get-ChildItemをベースに頑張る https://github.com/walnuts1018/Powershell/blob/main/Microsoft.PowerShell_profile.ps1 `a,l,h,S`しかできてない ```powershell= function ls { $paths = @() $rawoptions = @() $options = @() $prefix = @() $saffix = @() foreach ($a in $args) { if ($a.StartsWith("-") -And !($a.StartsWith("--"))) { $tmp = $a.ToCharArray() $rawoptions += $tmp[0..($tmp.length - 1)] } elseif ($a.StartsWith("--")) { #TODO } else { $paths += $a } } $rawoptions = $rawoptions | Select-Object -Unique foreach ($o in $rawoptions) { switch ($o) { #show hidden files "a" { $options += "-Force" } } } $prefix += '$result = New-Object -TypeName PSObject -Property @{Mode = "Mode"; LastWriteTime = "LastWriteTime"; Length = "Length"; Name = "Name"; LinkTarget = "LinkTarget"};' #show hidden files & dot files if ($rawoptions -contains "a") {} else { $saffix += '| Where-Object { $_.Name -NotMatch "^\." }' } #sort by size if ($rawoptions -contains "S") { $saffix += "| Sort-Object -Property Length -Descending" } #show file size in KB if ($rawoptions -contains "h") { $saffix += '|ForEach-Object {$result.Mode = $_.Mode; $result.LastWriteTime = $_.LastWriteTime; $result.Length = [string]([Math]::Round($_.Length/1024, 1, [MidpointRounding]::AwayFromZero))+"K"; $result.Name = $_.Name; $result.LinkTarget=$_.LinkTarget; $result}' } #show long format if ($rawoptions -contains "l") { $saffix += '| ForEach-Object {$result.Mode = $_.Mode; $result.LastWriteTime = $_.LastWriteTime; $result.Length = $_.Length; $result.Name = [string]($_.Name)+($null -ne $_.LinkTarget?" -> "+[string]($_.LinkTarget):""); $result} | Format-Table Mode,LastWriteTime,Length,Name' } else { $saffix += "| Select-Object Name | Format-Wide -AutoSize" } $command = "$($prefix -join " ") Get-ChildItem $($options -join " ") $($paths -join " ") $($saffix -join " ")" if (Test-Path Variable:PROFILE_DEBUG) { write-host $command } $command | Invoke-Expression } ```