gsamerica
gsamerica

Reputation: 153

Making PowerShell's Start-Transcript more compact

I'm using PowerShell to navigate through a directory and record the current file path when I reach a file that fits certain criteria. In my example, it's a file that starts with 2 and has two digits following it:

Start-Transcript -path C:\test.txt
Get-ChildItem -recurse | Where-Object {$_.Name -match "2\d\d"}
Stop-Transcript

Before switching to PowerShell I was just using the command line,

dir *2??*.doc /b /s /A:-D > C:\test.txt

Which picked up a bunch of stuff that I didn't want. However, the benefit of this command was that my text file was compact, with the entire file path of each file on one line:

C:\folder1\234.doc
C:\folder1\folder2\235.doc

The transcript function in Powershell is far larger:

Start time: 20170512141758
...
**********************
Transcript started, output file is C:\test.txt


    Directory: C:\folder1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        12-May-17     14:07              0 234.doc


    Directory: C:\folder1\folder2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        12-May-17     14:05              0 235.doc
**********************
Windows PowerShell transcript end
End time: 20170512141758
**********************

How do I make my PowerShell transcript as compact as the command line transcript?

Upvotes: 0

Views: 839

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

Pipe you're output directly to Out-File instead of using Start-Transcript:

Get-ChildItem -Recurse | Where-Object {$_.Name -match "2\d\d"} |Select -Expand FullName |Out-File C:\test.txt

The Select (alias for Select-Object) command after Where-Object makes sure we only get the full file path in the output file

Upvotes: 2

Related Questions