Advertisement
Wasif_Hasan_

CMDLikeDir.ps1

Aug 5th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # CMDLikeDir.ps1
  2. # Show Dir output like CMD in Powerhsell
  3. If ($args[0] -eq "") {Throw "Path is required."; Exit}
  4. $path = $args[0]
  5. $exist = Test-Path $path
  6. If (!$exist) {Throw "The directory does not exist."; Exit}
  7. $Files = 0
  8. $Dirs = 0
  9. $report = ""
  10. $Letter = "$($path.SubString(0,1))"
  11. $Vol = Get-WMIObject -Query "Select VolumeName From Win32_LogicalDisk Where Caption='$($Letter):'" | ForEach {$_.VolumeName}
  12. $Serial = Get-WMIObject -Query "Select VolumeSerialNumber From Win32_LogicalDisk Where Caption='$($Letter):'" | ForEach {$_.VolumeSerialNumber}
  13. $FreeSpace = Get-WMIObject -Query "Select FreeSpace From Win32_LogicalDisk Where Caption='$($Letter):'" | ForEach {$_.FreeSpace}
  14. $report += " Volume in drive $($Letter) is $($Vol)`n Volume Serial Number is $($Serial)`n`n Directory of $($path)`n`n"
  15. Get-ChildItem -Path $path | Sort-Object Name | ForEach {
  16.   $IsDir = $_.Attributes | Select-String "Directory" -Quiet
  17.   If ($IsDir) {$Dir = "`<DIR`>`t"; $Dirs += 1} Else {$Dir = "`t"; $Files += 1}
  18.   $report += "$($_.LastWriteTime)`t$($Dir)$($_.Length)`t$($_.Name)`n"
  19.   $Size += $_.Length
  20. }
  21. $report += "`t$($Files) File`(s`)`t$($Size) bytes`n`t$($Dirs) Dir`(s`)`t$($FreeSpace) bytes free`n`n"
  22. Write-Host $report
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement