Author Posts

July 24, 2015 at 2:42 pm

I am trying to figure out how to expand my custom objects that I created using [pscustomobject].

The output looks like this:

RenamedFrom RenamedTo
———– ———
PS Test 1... PS Test 1...

How do I get the output to appear as:

RenamedFrom RenamedTo
———– ———
PS Test 1 PS Testing 1
PS Test 2 PS Testing 2
PS Test 3 PS Testing 3

Thanks everyone

July 24, 2015 at 2:46 pm

Pipe it to Format-Table -AutoSize. 🙂 (In the latest versions of PowerShell 5.0, PowerShell does this automatically for the first few objects in the pipeline.)

July 25, 2015 at 3:47 am

Is the ; you show in your output part of the data?

July 26, 2015 at 11:11 am

Here is a portion of my code:

 
[pscustomobject] @{
    'RenamedFrom' = $OldName ;
     'RenamedTo' = $NewName
} 

How would I pipe this to Format-Table?

Richard, I thought the semicolon is supposed to be part of the syntax to delimit the next the object?

July 26, 2015 at 3:01 pm

You're not doing anything with the data you collected above. In this sample, I read in a CSV file with the column headings of OldName and NewName.

$results = Import-Csv -Path C:\Ephemeral\filenames.csv | foreach {
    [PSCustomObject]@{
        RenamedFrom = $_.OldName
        RenamedTo = $_.NewName
    }
}
# sample outputs
$results
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII

July 26, 2015 at 8:50 pm

I am so absent minded sometimes. I forgot that I had used an out-string earlier in my code and saved the data to a variable using -outvariable so I can re-use that, as part of the final output later which is what the code I initial posted was. This must be why it cannot do what I want, because it is a string and not objects now.

Is there a way to save actual objects in a variable without piping to Out-String -OutVariable?

July 27, 2015 at 6:44 am

It's a little unclear what you're trying to accomplish. Are you trying to save the contents of an object to a file so it can be used later in another script or function? If that the case I would use ExportClixml.

$results | Export-Clixml -Path .\foo.xml -Encoding ASCII

July 29, 2015 at 11:42 am

Well kind of, but I like to avoid using files if I can use variables instead, but I don't think it is possible in this case.

I think I will just go with your idea, Bob.

Thanks