windowspowershellwindows-administration

How to Sort Out Drive Letters from Powershell Output


So I am trying to make a for loop that will optimize the drives based on anyone's drives in windows. I have the following command and know how to structure the for loop. I just need the command to jkust print the drive letters. Is there a way to do this with the Get-PSDrive command?

PS C:\WINDOWS\system32> Get-PSDrive -PSProvider FileSystem

Name           Used (GB)     Free (GB) Provider      Root                                                                                   CurrentLocation
----           ---------     --------- --------      ----                                                                                   ---------------
C                1311.02        550.33 FileSystem    C:\                                                                                   WINDOWS\system32
D                 557.70        140.94 FileSystem    D:\
E                   0.22          0.46 FileSystem    E:\
F                   0.00        369.97 FileSystem    F:\
H                   0.23          1.14 FileSystem    H:\
J                1676.01          0.64 FileSystem    J:\
O                 899.72         31.79 FileSystem    O:\

How can I just print the drive names without the column name?


Solution

  • there are two ways to get the drive info you my seem to want from that cmdlet. one gets just the letter [the .Name, ex = C], and the other gets the root of the drive [the .Root, ex = C:\].

    drive letter only ...

    (Get-PSDrive -PSProvider FileSystem).Name
    # result is an array of just the drive letters
    #    C, D, E, etc.
    

    the root of the drives ...

    (Get-PSDrive -PSProvider FileSystem).Root
    # result is an array of drive roots
    #    C:\, D:\, E:\, etc.