none
Powershell Make an Array like a CSV does. RRS feed

  • Question

  • Quick question I've done a little google foo and I'm not finding anything that explains it really well so far.

    I'm attempting to make a powershll object or array that looks a little something like this

    Address GroupName OtherInfo

    111 Neverland NeverlandShadowallusers WhateverelseIfeellike

    Now, I know I could accomplish this easily by just writing a CSV and using the import-CSV powershell command but I would like, if possible to encapsulate all of the data into one spot in the powershell script itself so it's contained.

    Essentially I got tired of waiting for another team to do their job and so now I'm doing it for them. Occasionally we have users that transfer in our environment, when they transfer the address property on their account changes and I need to daily using SCORCH compare all users in the site shadow group against an address to make sure they have the correct permissions for the site they work at. 

    IF it's not possiblem, then I'll just go the import-CSV route I would just really like to be able to not HAVE to store a csv somewhere to update, I am however willing to be persuaded that would be better practice as it could be managed by other people leaving the script in lock down. 


    Please remember, if you see a post that helped you please click "Vote As Helpful" and if it answered your question, please click "Mark As Answer"

    Wednesday, November 11, 2015 1:28 PM

Answers

  • Hi,

    Here's some examples of how to create an object:

    #v2 or better
    $props = @{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }
    
    New-Object PsObject -Property $props |
        Select Address,GroupName,OtherInfo
    
    #v3 or better
    $props = [ordered]@{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }
    
    New-Object PsObject -Property $props
    
    #v4 (might work in v3, I don't remember)
    [pscustomobject]@{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }


    • Marked as answer by Jordan Benzing Wednesday, November 11, 2015 1:48 PM
    Wednesday, November 11, 2015 1:42 PM

All replies

  • Hi,

    Here's some examples of how to create an object:

    #v2 or better
    $props = @{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }
    
    New-Object PsObject -Property $props |
        Select Address,GroupName,OtherInfo
    
    #v3 or better
    $props = [ordered]@{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }
    
    New-Object PsObject -Property $props
    
    #v4 (might work in v3, I don't remember)
    [pscustomobject]@{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
    }


    • Marked as answer by Jordan Benzing Wednesday, November 11, 2015 1:48 PM
    Wednesday, November 11, 2015 1:42 PM
  • Awesome.

    That completely adds in the piece I was missing from what some of the other examples.

    Most other examples I saw were trying to create the object ahead of putting the data in and it just got grumpy anytime I tried to add the data after object creation. 


    Please remember, if you see a post that helped you please click "Vote As Helpful" and if it answered your question, please click "Mark As Answer"

    Wednesday, November 11, 2015 1:49 PM
  • Just to further my example a bit more, this is how I generally create multiple objects in one go:

    $hashArr = @(
        @{
        Address = '1234 Some Street'
        GroupName = 'Some Cool Group Name'
        OtherInfo = 'Likes long walks on the beach'
        },
        @{
        Address = '5678 Other Street'
        GroupName = 'Other Cool Group Name'
        OtherInfo = 'Hates long walks on the beach'
        },
        @{
        Address = '9999 That Blvd'
        GroupName = 'Not Cool Group Name'
        OtherInfo = 'Spelunking enthusiast'
        }
    )
    
    $hashArr | ForEach-Object { New-Object PsObject -Property $_ } |
        Select Address, GroupName, OtherInfo


    Wednesday, November 11, 2015 1:57 PM