SOLVED

Powershell > Output to .txt as one string

%3CLINGO-SUB%20id%3D%22lingo-sub-84322%22%20slang%3D%22en-US%22%3EPowershell%20%26gt%3B%20Output%20to%20.txt%20as%20one%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-84322%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%26nbsp%3Bpowershell%20script%20that%20loops%20a%20fileshare%20and%20reports%20the%20properties%20of%20all%20the%20files.%3C%2FP%3E%3CP%3EThis%20part%20reports%20to%20a%20.txt%20file%3A%3C%2FP%3E%3CPRE%3EGet-ChildItem%20-Recurse%20%24source%20%7C%20%20%3F%7B-not%20%24_.PSIsContainer%7D%20%7C%20ForEach-Object%20%7BAudit-File%20%24_%7D%20%7C%20Sort-Object%20fullname%20%7C%20Select%20FullName%2CCreationTime%2CLastWriteTime%2CLength%2COwner%20%7C%20Out-File%20C%3A%5Cfilename.txt%3C%2FPRE%3E%3CP%3EThe%20output%20is%20something%20like%20this%3A%3C%2FP%3E%3CPRE%3EFullName%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20C%3A%5Ctemp%5Ctestfile%0ACreationTime%20%20%20%20%20%20%20%20%20%20%20%20%3A%2030-6-2017%2013%3A34%3A16%0ALastWriteTime%20%20%20%20%20%20%20%20%20%20%20%3A%208-9-2014%2017%3A15%3A57%0ALength%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%205752832%0AOwner%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20BLA%5Cbla.bla%0AAuthor%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20%3C%2FPRE%3E%3CP%3EBut%20I%20would%20like%20to%20have%20something%20like%20this%3A%3C%2FP%3E%3CPRE%3EFullName%2CC%3A%5Ctemp%5Ctestfile%2CCreationTime%2C30-6-2017%2013%3A34%3A16%2CLastWriteTime%2C8-9-2014%2017%3A15%3A57%2CLength%2C5752832%2COwner%2CBLA%5Cbla.bla%2CAuthor%20%3C%2FPRE%3E%3CP%3ESo%20one%20line%20for%20each%20record.%3C%2FP%3E%3CP%3EI%20cannot%20use%20a%20.csv%20as%20this%20cannot%20handle%20things%20like%20%22%C3%A9%22%2C%20etc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-84322%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%20Commands%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-84352%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20%26gt%3B%20Output%20to%20.txt%20as%20one%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-84352%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20this%20script%20for%20output%20with%20txt%20file%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24Details%3DGet-ChildItem%20-Recurse%20%24source%20%7C%20%20%3F%7B-not%20%24_.PSIsContainer%7D%20%7C%20ForEach-Object%20%7BAudit-File%20%24_%7D%20%7C%20Sort-Object%20fullname%20%7C%20Select%20FullName%2CCreationTime%2CLastWriteTime%2CLength%2COwner%20%0A%20%0A%20%24Outputs%20%3D%40()%0A%20ForEach(%24Detail%20in%20%24Details)%0A%20%0A%20%7B%0A%0A%20%24Output%20%3D%20%22Fullname%2C%22%2B%24Detail.FullName%2B%22%2CCreationTime%2C%22%2B%24Detail.CreationTime%2B%22%2CLastWriteTime%20%2C%22%2B%20%24Detail.LastWriteTime%2B%22%2CLength%20%2C%22%2B%22%24Detail.Length%22%2B%20%22%2COwner%2C%22%2B%24Detail.Owner%0A%20%24Outputs%20%2B%3D%24Output%0A%0A%20%7D%0A%20%24Outputs%20%7C%20Out-File%20C%3A%5Cfilename.txt%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-84346%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20%26gt%3B%20Output%20to%20.txt%20as%20one%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-84346%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20change%20the%20encoding%20to%20UTF8%20or%20other%2C%20as%20necessary.%20Unicode%20works%20just%20fine%20with%20%C3%A9nter%20for%20example.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-84338%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20%26gt%3B%20Output%20to%20.txt%20as%20one%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-84338%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%3C%2FP%3E%3CP%3EIf%20I%20do%20so%2C%20my%20text%20will%20be%20changed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20example%20%22%C3%A9nter%22%20will%20be%20changed%20to%20%22%3Fnter%22.%20Has%20something%20to%20do%20with%20utf%20encoding.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-84332%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20%26gt%3B%20Output%20to%20.txt%20as%20one%20string%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-84332%22%20slang%3D%22en-US%22%3E%3CP%3EUse%20the%20-Encoding%20parameter%20with%20Export-CSV%2C%20it%20works%20just%20fine.%20For%20example%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExport-Csv%20-NTI%20blabla.csv%20-Encoding%20Unicode%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

I have a powershell script that loops a fileshare and reports the properties of all the files.

This part reports to a .txt file:

Get-ChildItem -Recurse $source |  ?{-not $_.PSIsContainer} | ForEach-Object {Audit-File $_} | Sort-Object fullname | Select FullName,CreationTime,LastWriteTime,Length,Owner | Out-File C:\filename.txt

The output is something like this:

FullName                  : C:\temp\testfile
CreationTime            : 30-6-2017 13:34:16
LastWriteTime           : 8-9-2014 17:15:57
Length                    : 5752832
Owner                     : BLA\bla.bla
Author                    : 

But I would like to have something like this:

FullName,C:\temp\testfile,CreationTime,30-6-2017 13:34:16,LastWriteTime,8-9-2014 17:15:57,Length,5752832,Owner,BLA\bla.bla,Author 

So one line for each record.

I cannot use a .csv as this cannot handle things like "é", etc.

 

4 Replies
Highlighted

Use the -Encoding parameter with Export-CSV, it works just fine. For example:

 

Export-Csv -NTI blabla.csv -Encoding Unicode

Highlighted

Hi @Vasil Michev

If I do so, my text will be changed.

 

For example "énter" will be changed to "?nter". Has something to do with utf encoding.

Highlighted

You can change the encoding to UTF8 or other, as necessary. Unicode works just fine with énter for example.

Highlighted
Best Response confirmed by Mike Jansen (Regular Contributor)
Solution

Try this script for output with txt file

 

$Details=Get-ChildItem -Recurse $source |  ?{-not $_.PSIsContainer} | ForEach-Object {Audit-File $_} | Sort-Object fullname | Select FullName,CreationTime,LastWriteTime,Length,Owner 
 
 $Outputs =@()
 ForEach($Detail in $Details)
 
 {

 $Output = "Fullname,"+$Detail.FullName+",CreationTime,"+$Detail.CreationTime+",LastWriteTime ,"+ $Detail.LastWriteTime+",Length ,"+"$Detail.Length"+ ",Owner,"+$Detail.Owner
 $Outputs +=$Output

 }
 $Outputs | Out-File C:\filename.txt