How to expand [pscustomobject] properties

This topic contains 7 replies, has 4 voices, and was last updated by  I Am Sir Ask Alot 2 years, 4 months ago.

  • Author
    Posts
  • #27869

    I Am Sir Ask Alot
    Participant

    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

  • #27870

    Dave Wyatt
    Moderator

    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.)

  • #27878

    Richard Siddaway
    Moderator

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

  • #27889

    I Am Sir Ask Alot
    Participant

    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?

  • #27892

    Bob McCoy
    Participant

    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
    
  • #27900

    I Am Sir Ask Alot
    Participant

    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?

  • #27912

    Bob McCoy
    Participant

    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
    
  • #27995

    I Am Sir Ask Alot
    Participant

    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

You must be logged in to reply to this topic.