I am exporting data to a csv and for some reason the #{} are transferring over. Here is a sample script.
Get-VM VM | Select Name, #{N="DSFree";E={$_ | Get-Datastore | Select FreeSpaceMB }} | Export-Csv c:\temp\info.csv
The output of the DSFree column looks like this: #{FreeSpaceMB=686704}
How can I stop the #{} from exporting?
Thanks in advance.
I can't try your specific example, but typically -ExpandProperty is the answer:
Get-VM VM | Select Name, #{N="DSFree";E={$_ | Get-Datastore | Select -expandProperty FreeSpaceMB }} | Export-Csv c:\temp\info.csv
While #EBGreen's answer made me learn something, there is an easier way I believe in this case:
Get-VM VM | Select Name, #{N="DSFree";E={($_ | Get-Datastore).FreeSpaceMB }} | Export-Csv c:\temp\info.csv
Related
Get-VM |
Select Name, Operatingsystem, VMHost, PowerState,
#{N="Datastore"; E={$_ |
Get-Datastore}} |
Out-gridview
I ran this command. It will return and output a grid view with all rows filled in.
However, the field "OperatingSystem" returns a blank column, nothing there.
Untested, but I think you can do this:
Get-VM | Select-Object Name,
#{Name = 'Operatingsystem'; Expression = {$_.Guest.OsFullName}},
VMHost, PowerState,
#{Name = 'Datastore'; Expression = {$_ | Get-Datastore}} |
Out-GridView
You're looking for the "Guest" property I think. FYI you can also do this to view everything if you're not sure of the exact name
Get-VM |
Select-Object -Property * |
Out-gridview
I want to keep the output of this command on the same line:
((Get-ADUser -filter {employeetype -eq "Employee"}).SamAccountName) | Sort-Object | Get-ADUser | ForEach-Object {$_.Name,$_.Department,$_mail}
Currently the output shows up like this:
James Roberts
Accounting
jroberts#email.tld
But, I need to have it show up like:
James Roberts Accounting jroberts#email.tld
I've also tried using (based on a suggestion I found):
((Get-ADUser -filter {employeetype -eq "Employee"}).SamAccountName) | Sort-Object | Get-ADUser | ForEach-Object {$_.Name;$_.Department;$_mail}
but, I get the same three lines of output and not one line.
If you want to create customized objects:
… | Select-Object -Property Name,Department,Mail
If you want to make strings:
… | ForEach-Object { $_.Name,$_.Department,$_.Mail -join " " }
If you just want to display the table nicely (not going to use this output afterwards):
… | Format-Table -Property Name,Department,Mail -AutoSize
I have a very simple script where I am listing all the websites on my server. I am using this output to create custom columns in Format-Table to show required information. Then I want to represent this whole info in a HTML file using ConvertTo-Html cmdlet.
But when I run below code, it just creates garbage. However, if I do an Out-File to a .txt (no HTML conversion), it records correct information. Also, if I only display the results on screen, it still shows correct information.
cls
Import-Module WebAdministration
Get-Website |
Format-Table #{n = 'Site_Name'; e = {$_.Name}},
#{n = 'Physical Path'; e = {$_.physicalpath}},
#{n = 'Version'; e = {($_.physicalpath.split("\"))[-1]}} -AutoSize |
ConvertTo-Html |
Out-File -FilePath D:\test.html
Any help will be greatly appreciated.
Format-Table creates formatted output that is suitable for displaying it in the console, but not for passing it to ConvertTo-Html.
Replace Format-Table with Select-Object:
Get-Website |
Select-Object #{n='Site_Name';e={$_.Name}},
#{n='Physical Path';e={$_.physicalpath}},
#{n='Version';e={($_.physicalpath.split("\"))[-1]}} |
ConvertTo-Html |
Out-File -FilePath D:\test.html
and the problem will disappear.
You can see the difference if you pass the Format-Table and the Select-Object output into Get-Member:
... | Format-Table ... -AutoSize | Get-Member
... | Select-Object ... | Get-Member
I have a collection of objects with properties: ProductName and PartName. The content of collection is output to a file first:
$colProducts | sort-object ProductName | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File myProducts.txt
So far so good. However, I have trouble to append a text message to the result file like this:
Add-Content myProducts.txt "`nParts in more than one Product`n"
I found that the appended text is not readable at the end. One thing I notice is that the output of the first collection to a file is in Unicode, and the second one code (add-content) is in ASCII if only to a new file.
After this, I would like to continue to add the following information the same result file:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File myProducts.txt
The above codes will overwrite to the result file. I need to append to the file. Greatly appreciate help!
Update: It is good to know -Append option. How about Add-Content? It seems adding some unreadable chars to the file after Out-File from collection.
I would first try:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File -Append myProducts.txt
And then look at this to get a feel for what you were encountering.
Essentially, Out-File (and Out-File -Append) gives you Unicode by default and Add-Content gives ASCII by default. My advice would be stick to the same command and you shouldn't have a problem.
And, of course, help Out-File -Detailed! Always check out the powershell examples because they help a great deal, not just to figure out their common usage, but to grok them as well.
Try:
$colProducts | Group-object PartName | sort-object PartName | `
Where-Object {$_.Count -gt 1 } | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-File -Append myProducts.txt
Another option:
$colProducts | sort-object ProductName | `
Select-object ProductName PartName | `
Format-Table -autosize ProductName, PartName | `
Out-String | Add-Content myProducts.txt
Add-Content myProducts.txt "`nParts in more than one Product`n"
Get-WmiObject -list | where-object {$_.name -match "win32"} | Select-Object
name,methods,properties
This displays the name, methods and properties of each Win32 class and I wanted to get this information into a CSV file.
The following however outputs doesn't output the same information.
Get-WmiObject -list | where-object {$_.name -match "win32"} | Select-Object
name,methods,properties | Export-CSV "c:\output.csv"
How can I do this?
(Updated my script as it had an error.)
You need to do some extra manual work and make sure you expand the names and join them by some delimiter:
$methods = #{n='Methods';e={ ($_.Methods | select -expand Name) -join ';'}}
$properties = #{n='Properties';e={ ($_.Properties | select -expand Name) -join ';'}}
Get-WmiObject -List |
Where-Object {$_.Name -like "Win32_*"} |
Select-Object Name,$methods,$properties |
Export-Csv .\win32.csv -NoTypeInformation
The problem here is that each WMI Object has properties that themselves are arrays and Output-CSV can't really handle that.
To fix this, you'd need to handle the arrays of arrays explicitly.
WHat specifically do you want to be output?