Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for drives.ps

(view source code of drives.ps as plain text)

  1. param(
  2. 	[switch]$Available,
  3. 	[switch]$Used,
  4. 	[switch]$h,
  5. 	[switch]$V
  6. )
  7.  
  8. if ( $h ) {
  9. 	Write-Host
  10. 	Write-Host "Drives.ps1,  Version 1.01"
  11. 	Write-Host "List all available and/or used drive letters"
  12. 	Write-Host
  13. 	Write-Host "Usage:  " -NoNewline
  14. 	Write-Host "./Drives.ps1  [ -Available | -Used ]  [ -V ]" -ForegroundColor White
  15. 	Write-Host
  16. 	Write-Host "Where:  " -NoNewline
  17. 	Write-Host "-Available    " -ForegroundColor White -NoNewline
  18. 	Write-Host "displays " -NoNewline
  19. 	Write-Host "available " -ForegroundColor White -NoNewline
  20. 	Write-Host "drive letters (default: all)"
  21. 	Write-Host "        -Used         " -ForegroundColor White -NoNewline
  22. 	Write-Host "displays drive letters " -NoNewline
  23. 	Write-Host "in use    " -ForegroundColor White -NoNewline
  24. 	Write-Host "(default: all)"
  25. 	Write-Host "        -V            " -ForegroundColor White -NoNewline
  26. 	Write-Host "explains what is displayed       (see notes)"
  27. 	Write-Host
  28. 	Write-Host "Notes:  This script doesn't require elevated privileges."
  29. 	Write-Host "        If either the " -NoNewline
  30. 	Write-Host "-Available " -ForegroundColor White -NoNewline
  31. 	Write-Host "or the " -NoNewline
  32. 	Write-Host "-Used " -ForegroundColor White -NoNewline
  33. 	Write-Host "switch is used, but not"
  34. 	Write-Host "        both, only the drive letters themselves are displayed, not the"
  35. 	Write-Host "        explaining text, unless the " -NoNewline
  36. 	Write-Host "-V " -ForegroundColor White -NoNewline
  37. 	Write-Host "switch is also used."
  38. 	Write-Host "        If both " -NoNewline
  39. 	Write-Host "-Available " -ForegroundColor White -NoNewline
  40. 	Write-Host "and " -NoNewline
  41. 	Write-Host "-Used " -ForegroundColor White -NoNewline
  42. 	Write-Host "are used, " -NoNewline
  43. 	Write-Host "-V " -ForegroundColor White -NoNewline
  44. 	Write-Host "is implied."
  45. 	Write-Host "        Though this script can be run in PowerShell on Linux, that"
  46. 	Write-Host "        would be useless because Linux doesn't use drive letters."
  47. 	Write-Host
  48. 	Write-Host "Written by Rob van der Woude"
  49. 	Write-Host "http://www.robvanderwoude.com"
  50. 	Exit 1
  51. }
  52.  
  53. # List all drive letters in use
  54. $useddrives = @( )
  55. Get-PSDrive -PSProvider FileSystem | ForEach-Object { $useddrives += $_.Name }
  56.  
  57. # List all drive letters still available
  58. $availabledrives = @( )
  59. [int][char]"A"..[int][char]"Z" | ForEach-Object {
  60. 	[string]$drive = [string][char][int]$_
  61. 	if ( -not ( $useddrives.Contains( "$drive" ) ) ) {
  62. 		$availabledrives += $drive
  63. 	}
  64. }
  65.  
  66. # Display list of drive letters in use
  67. if ( $Used -or -not $Available ) {
  68. 	if ( $V -or -not ( $Available -xor $Used ) ) {
  69. 		Write-Host "Drive Letters In Use:   " -NoNewline
  70. 	}
  71. 	$useddrives | ForEach-Object { Write-Host " $_" -NoNewline; Write-Host ":" -NoNewline }
  72. 	Write-Host
  73. }
  74.  
  75. # Display list of available drive letters
  76. if ( $Available -or -not $Used ) {
  77. 	if ( $V -or -not ( $Available -xor $Used ) ) {
  78. 		Write-Host "Available Drive Letters:" -NoNewline
  79. 	}
  80. 	$availabledrives | ForEach-Object { Write-Host " $_" -NoNewline; Write-Host ":" -NoNewline }
  81. 	Write-Host
  82. }
  83.  

page last modified: 2024-04-16; loaded in 0.0097 seconds