0

I would like to have the current volume name as part of a filename, e.g. bsps-on-c.txt for files in volume C:, bsp-on-d.txt for files in volume d and so on.

code is:

$v=volume
$col=@{expression="driveletter";label=""}
$v | where {$_.drivetype -like "Fixed" -and  $_.driveletter -notlike "" } | format-table $col

output is:


-
C

question is: How to get rid off empty lines and line with -

Expectation was:

only letter C and nothing more, no white spaces, no empty lines

mklement0
  • 382,024
  • 64
  • 607
  • 775
jomaber
  • 1
  • 2
  • 1
    If your plan is to use the _value_ and not just see it as _output_, you should either expand the property using `Select-Object -ExpandProperty` or `ForEach-Object -MemberName` – Maximilian Burszley Mar 25 '22 at 13:19
  • 1
    The linked duplicate discusses how to extract a single property value _as data_ - Mathias' answer shows one approach, Maximilian's comment mentions another. By contrast, `Format-*` cmdlets output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's output-formatting system - see [this answer](https://stackoverflow.com/a/55174715/45375). In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_. – mklement0 Mar 25 '22 at 13:19

1 Answers1

3

If you just want to output the drive letter on its own:

Get-Volume |Where-Object {$_.DriveType -like "Fixed" -and  $_.DriveLetter -notlike "" } |ForEach-Object DriveLetter

ForEach-Object DriveLetter will attempt to resolve the DriveLetter property on each input object and return its raw value.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I had to recognize, that the question was not clear enough. What I need ist the __current__ volume of the disk, usb-stick, DVD ..., where the actual script is running. I would like to have the _current_ volume name as part of a filename, e.g. bsps-on-c.txt for files in volume C:, bsp-on-d.txt for files in volume d and so on. – jomaber Mar 25 '22 at 17:56