Powershell Output File column too narrow - powershell

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

Related

Powershell export contains breaks and spaces [duplicate]

I have the following Powershell script that I am running in Powershell Core:
PS C:\> Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10
>> files.txt
Opening the file files.txt there are no columns for LastAccessTime, LastWriteTime - why is this and how do I amend the script so they are included in the text file?
Two asides:
>> is a (virtual) alias for Out-File -Append; if the intent is not to append (to a preexisting file) but to only capture the current command's (entire) output, use just > / Out-File without -Append.
Out-File and therefore also > / >> capture the for-display representation of the input objects in a file, which is not suitable for subsequent programmatic processing. For the latter, use a cmdlet that produces structured output, such as Export-Csv.
That said, it's reasonable to expect Out-File to capture all for-display data, given that a file rather than the display is the target.
Regrettably, as of PowerShell [Core] v7.0, Out-File / > / >> are (still) sensitive to the console (terminal) window's width and capture only as much as would currently fit on the screen.
You can use Out-File with the -Width parameter to work around that:
Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 |
Out-File -Width ([int]::MaxValue-1) files.txt # Add -Append, if needed.
Note: As of v7.0, [int]::MaxValue-1 is seemingly the highest value accepted; [int]::MaxValue is quietly ignored. Also, be sure not to use such a high value in Windows PowerShell, where each output line is unconditionally right-space-padded to that length, which would result in excessively large files.

Remove blank space from text file

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

How do I export these Active Directory User Properties to CSV? [duplicate]

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

Why is all the Powershell output not written to txt file

I have the following Powershell script that I am running in Powershell Core:
PS C:\> Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10
>> files.txt
Opening the file files.txt there are no columns for LastAccessTime, LastWriteTime - why is this and how do I amend the script so they are included in the text file?
Two asides:
>> is a (virtual) alias for Out-File -Append; if the intent is not to append (to a preexisting file) but to only capture the current command's (entire) output, use just > / Out-File without -Append.
Out-File and therefore also > / >> capture the for-display representation of the input objects in a file, which is not suitable for subsequent programmatic processing. For the latter, use a cmdlet that produces structured output, such as Export-Csv.
That said, it's reasonable to expect Out-File to capture all for-display data, given that a file rather than the display is the target.
Regrettably, as of PowerShell [Core] v7.0, Out-File / > / >> are (still) sensitive to the console (terminal) window's width and capture only as much as would currently fit on the screen.
You can use Out-File with the -Width parameter to work around that:
Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 |
Out-File -Width ([int]::MaxValue-1) files.txt # Add -Append, if needed.
Note: As of v7.0, [int]::MaxValue-1 is seemingly the highest value accepted; [int]::MaxValue is quietly ignored. Also, be sure not to use such a high value in Windows PowerShell, where each output line is unconditionally right-space-padded to that length, which would result in excessively large files.

Powershell output

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