Home > Back-end >  Powershell export result to txt with format
Powershell export result to txt with format

Time:06-09

I have a small script that looks for the two files with the smallest size and exports them to a txt. The problem is that the result is exported with a "bit weird format".

$checksize = gci -r| sort -descending -property length | select -last 2 name, length | Add-Content -Path C:\log.txt

And the result

@{Name=fileexample.txt; Length=3482342}
@{Name=server.iso; Length=548238474}

I would like only the names of the files and their size to appear.Does anyone know how I could do it?

Thanks. Good Day

CodePudding user response:

Try to use Out-File instead of Add-Content

CodePudding user response:

#sample path to csv file
$CsvFile = Import-CSV -Path C:\data.csv -Delimiter ";" -Header File,Length

ForEach ($u in $CsvFile)
{
$A = ($u.File).Replace("@{Name=","")
$B = (($u.Length).Replace("Length=","")).Replace("}","")
Write-Host $A $B
}

or export to csv

gci -Path C:\test -r| sort -descending -property length | select -last
2 name, length|Export-Csv C:\log.csv -NoTypeInformation
  • Related