0

I am trying to send Powershell write-host output into new file after executing the command this command creating a blank file

gci -r | % { Write-Host $_.Name,$_.FullName,$_.LastWriteTime } |ft  -wrap | out-file output.txt

i want FileName and File Full Path and Last Write Time in output file like this

170801072740IMG-20170622-WA0012 (1).jpg O:\170801072740IMG-20170622-WA0012 (1).jpg 1/14/2021 4:3
170801072756IMG-20170622-WA0014.jpg O:\170801072756IMG-20170622-WA0014.jpg 1/14/2021 4:35:22 PM
170801072818IMG-20170624-WA0028.jpg O:\170801072818IMG-20170624-WA0028.jpg 1/14/2021 4:35:22 PM

Excluding Properties Names:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----

also checked with antoher command which is Write-Output

gci -r | % { Write-Output $_.Name,$_.FullName,$_.LastWriteTime } >> output2.txt

this command is redirecting output but the output is not in right format

Output file:

Tuesday, March 9, 2021 1:14:42 AM
151104051548IMG_20151103_112015.jpg
O:\151104051548IMG_20151103_112015.jpg
Friday, March 12, 2021 8:40:15 PM
151104051558IMG_20151103_123234.jpg
O:\151104051558IMG_20151103_123234.jpg
Thursday, January 14, 2021 4:35:16 PM
151104051610IMG_20151103_123249.jpg
O:\151104051610IMG_20151103_123249.jpg
Thursday, January 14, 2021 4:35:16 PM

i have also tried with Start-Transcript method but again output is not in right format

Start-Transcript Output file output

Is there any way to fix this issue?

4
  • 1
    Why not drop Write-Host completely and just write the values directly to file? gci -r | % { $_.Name,$_.FullName,$_.LastWriteTime -join ' ' } |Out-File .\path\to\file.txt Commented Mar 12, 2021 at 16:58
  • Great thanks for Your Help Commented Mar 12, 2021 at 17:05
  • 1
    gci -r | ft Name, FullName, LastWriteTime >> result.txt Commented Mar 12, 2021 at 17:16
  • @FletcherF1 I need only values w/o properties names Name and Full Name and Last Write Time Commented Mar 12, 2021 at 17:25

1 Answer 1

2
gci -r -file | % { "$($_.Name) $($_.FullName) $($_.LastWriteTime)" >> G:\Test\Output.txt } 

I added the -file parameter as there is no need for directories since they are included in the full path.

Sample Output:

Get-FileMetaDataReturnObject.ps1 G:\BEKDocs\Scripts\Functions\Get-FileMetaDataReturnObject.ps1 09/20/2018 20:03:57
Get-InstalledSoftware.ps1 G:\BEKDocs\Scripts\Functions\Get-InstalledSoftware.ps1 05/02/2020 12:24:47
Invoke-ExerciseTimer.ps1 G:\BEKDocs\Scripts\Functions\Invoke-ExerciseTimer.ps1 01/19/2017 15:01:49
New-Shortcut.txt G:\BEKDocs\Scripts\Functions\New-Shortcut.txt 01/15/2017 11:50:33
Test-RoboForm-Function.ps1 G:\BEKDocs\Scripts\Functions\Test-RoboForm-Function.ps1 02/24/2021 20:32:25
Get-DisabledServicesV1-00.ps1 G:\BEKDocs\Scripts\Get-DisabledServices\Get-DisabledServicesV1-00.ps1 01/13/2018 16:39:09
Get-DisabledServicesV2-00.ps1 G:\BEKDocs\Scripts\Get-DisabledServices\Get-DisabledServicesV2-00.ps1 01/14/2018 12:31:56
Get-WinVer-V-2-0.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-2-0.ps1 03/12/2019 21:14:37
Get-WinVer-V-3-0.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-0.ps1 12/10/2019 20:03:38
Get-WinVer-V-3-1.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-1.ps1 12/11/2019 09:17:17
Get-WinVer-V-3-2.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-2.ps1 01/15/2020 20:53:11
Sign up to request clarification or add additional context in comments.

2 Comments

thats what i want but did not understand that -file parameter
I recommend changing ` >> G:\Test\Output.txt }` to } | Set-Content G:\Test\Output.txt unless you really need to append the data. Otherwise PowerShell has to open the file to write, write data, and close the file every iteration of the ForEach-Object loop (once per file passed to it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.