Page 1 of 1

GCI / dir command : how to get plain (unembellished) output

Posted: 2021-Jul-26, 9:32 pm
by MigrationUser
25 Jan 2019 04:33
be

In batch dir /b, wildcard ? unavoidably snags some unacceptable results;
therefore in batch I need to use instead an equivalent powershell command.
This line
>powershell "dir -directory -include ???-?? |select fullname"
gives correct results.
However, they are preceded by two preamble lines

FullName
--------

bad for the further processing.
Question is how to get clean output
(not how to clean the output).

Last edited by be (25 Jan 2019 04:44)

----------------------------

# 25 Jan 2019 11:46
Simon Sheppard


You can remove those lines by piping to format-table -HideTableHeaders

so

Code: Select all

PS C:\> dir -directory -include ???-?? | select fullname | format-table -HideTableHeaders
However it's important to realise that while the above is fine as a way to format things for display on the screen, in PowerShell everything is an object and that is a very very useful thing, once you pipe the output through format-table you have a bunch of strings with spaces and tabs and all kinds of crud that becomes difficult to work with, it becomes like going back to CMD.

A better approach - you can put the list of files into a variable with:

Code: Select all

$files = dir -directory -include ???-?? | select fullname
And then display the list with:

Code: Select all

$files
Even though this looks like one simple column of strings, it is not, we are still working with an object that can have multiple properties and methods, it just happens that by default it only displays the FullName property

To display the list without any header line, explicitly request just that one property:

Code: Select all

$files.FullName
I think everyone starting with PowerShell thinks just give me the string I know what to do with that, but once you get used to working with objects you realise thats where all the "Power" in PowerShell is.

----------------------------

#25 Jan 2019 12:17
XPlantefeve


To answer precisely your question : select -ExpandProperty fullname

But Simon is right: if you're trying to manipulate strings, you don't have the correct mindset. Forget about batch and jump on the PowerShell bandwagon.

----------------------------

#25 Jan 2019 15:27
be


Reply, everything in it, is excellent.

----------------------------

#25 Jan 2019 15:51
be
XPlantefeve wrote:

select -ExpandProperty fullname
Good.
XPlantefeve wrote:

if you're trying to manipulate strings
I seek to avoid having to manipulate
XPlantefeve wrote:

jump on the PowerShell bandwagon
On target nonetheless. Taken to heart.

----------------------------

#25 Jan 2019 16:54
Simon Sheppard
XPlantefeve wrote:

To answer precisely your question : select -ExpandProperty fullname
Good point, I didn't realise that also removed the headers - learn something new every day!