I'm having a little issue with controlling output in ps. Here's the line of code in question;
$result|sort-object cn | format-table -wrap -autosize
If I append
| out-file $logfile
the last column of my output is truncated. Now I know that if I changed the width of my console session and run the script again, my output is fine, but there must be a better way of doing this? I also attempted to use add-content but I think I must be missing something as the expression isn't being evalulated correctly and I just get a series of references to system-object in my logfile.
You can use the -width parameter for the out-file cmdlet. You might try out-file -width 500 so nothing gets truncated.
The best way I've determined so far is to use Out-String with a -Width longer than you expect the entire line to be:
$result | Format-Table -Autosize | Out-String -Width 4096
The only problem with the above is that it will pad the entire line with spaces. In order to get around that, add the -Stream switch and .Trim() each line:
$result | Format-Table -Autosize | Out-String -Width 4096 -Stream | %{ $_.Trim() }
This is also nice for piping the results to the clipboard with clip.exe (if I don't have the PSCX module installed with the Out-Clipboard command):
$result | Format-Table -Autosize | Out-String -Width 4096 -Stream | %{ $_.Trim() } | clip.exe
Related
I have a script to import the names of the virtual machines from hyper-v to a txt file.
The problem is that sometimes the names have blank spaces on the back, and when I try to turn them off or on with the script it does not find those machines because of the blank spaces.
Any way to remove the blank spaces from the file?
$vm = Get-VM | select name | Out-File -FilePath $ListVM
(Get-Content $ListVM | Select-Object -Skip 3) | ? {$_.trim() -ne ""} | Set-Content $ListVM
Withstanding Santiago's relevant alternate comment/approach. the cause of your issue may be:
Generally the Out-* cmdlets will force objects through PowerShell's for-display formatting system. You can use Set-Content from the start to avoid this, however you cannot simply select the name property doing so will create objects with the single property named "name". When Set-Content sees that it will write hash/object syntax to the file trying to represent the object, like:
#{Name=MachineName}
To avoid this simply expand the property you want to store in the file:
Get-VM |
Select-Object -ExpandProperty Name |
Set-Content -FilePath $ListVM
Or:
(Get-VM).Name | Set-Content -FilePath $ListVM
Note: you are assigning $VM to the output, however that will result in $VM being null. I'm not sure what the intent is, but if you want the resulting list stored in the variable add the -PassThru parameter like:
$vm = (Get-VM).Name | Set-Content -FilePath $ListVM -PassThru
I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services
PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture
Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation
I am trying to list current installed applications and the DisplayVersion keeps getting its column shortened when I want it to show as normal on one line. It runs fine when running the code in a PS session but when running from a ps1 script file, the output for DisplayVersion is 1 character wide with NewLines for each integer.
$FormatEnumerationLimit = -1
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object Publisher, DisplayName, DisplayVersion |
Where {$_.DisplayName} |
Sort-Object -Property Publisher,DisplayName,DisplayVersion |
Format-Table -AutoSize -Wrap > $FullFileName
Considering the exact suggestion from LotPings, I am making this as an answer because it makes more sense to dump it in File using out-file & use Encoding instead of redirection.
Replace this:
Format-Table -AutoSize -Wrap > $FullFileName
To:
Out-File $FullFileName -Width 300 -Encoding Ascii
I'm seriously sleep deprived and stressed so it is probably extremely simple yet I still can't manage to figure it out.
I want to get an export of various things like services, processes, firewallrules, local users, whatever I need as a CSV but whenever I export it as such the csv only has 1 column in Excel when I open it.
I just want the output to be separated by columns so I can have a simple overview in Excel.
Get-Service | Sort-Object Status | Format-Table -AutoSize | Out-String -Width 10000 | Out-File -Append "C:\temp\Test.csv"
But this is the output I get:
Services
This is what happens if I use a Export-CSV CMD: Services
PowerShell's default CSV delimiter is a comma. To make Excel read the CSV properly, change it to a semicolon using the -Delimiter parameter:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -Delim ";"
Or use the -UseCulture switch (thanks #JosefZ), which uses the same region-based ListSeperator-setting as Excel:
Get-Service | sort Status | Export-Csv "C:\temp\Test.csv" -NoTypeInfo -UseCulture
Why not just export to csv then?
Get-Service | Select Name,DisplayName,Status | Sort-Object Status | Export-Csv "C:\temp\Test.csv" -NoTypeInformation
I wrote a Powershell script that looks for a string (such as ERROR) in log files and grabs those lines and outputs those lines to a file, for simpler reading and such (the industry I'm in has VERY large log files), but I'm having an issue. Before, when the (relevant) part of the code looked like this:
Select-String -Path "$file" -Pattern "$string" -CaseSensitive | Out-File -filepath $filepath
It would output the file path, the line number, and then the actual line, making for a very cluttered file. Well I only needed the line and the line number, so I did this:
Select-String -Path "$file" -Pattern "$string" -CaseSensitive | Select-Object -Property LineNumber,Line | Out-File -filepath $filepath
Which would return lines looking like this:
978 2017-07-10 10:46:11,288 ERROR [Music...
That is the line number then the line, with the line only totaling 35 characters.
Before I piped Select-String to Select-Object, the script would output the whole line, but now with Select-Object it omits some output. I tried adding -verbose parameters to both Select-String and Select-Object, but that did nothing.
Can you try this :
Select-String -Path "test.xml" -Pattern "ERROR" -CaseSensitive | ft -Property LineNumber,Line -Wrap | Out-File -FilePath c:\out.txt
The reason for your problem is screen buffer length(increasing powershell screen buffer width) ,you can change it as well but the above snippet is simpler and effective