Set all values of keys in pscustomobject to single value

hello there!

Can someone help me on below:

I am trying to assign all values of keys in PSCustomObject to “N/A”. Here’s what I was trying to do:

           $Object = [PSCustomObject]@{

            GivenName        = $GivenName
            Surname          = $Surname
            EmailAddress     = $Surname
            Office           = $Office
            Title            = $Title

            }

Now, I need all the sub keys to be “NA”. How can I achieve this ?

Thanks.

$Object.psobject.properties | ForEach-Object {$_.Value = 'N/A'}

2 Likes

@matt-bloomfield , Hello Matt, Thanks for answering.

But, I am getting error when I run the command

$Object.psobject.properties : The term ‘$Object.psobject.properties’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.

At line:1 char:5
+     $Object.psobject.properties | ForEach-Object {$_.Value = ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($Object.psobject.properties:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

can you please help, what went wrong?

Really? I get exactly what you’ve asked for:

$Object = 
[PSCustomObject]@{
    GivenName    = $GivenName
    Surname      = $Surname
    EmailAddress = $Surname
    Office       = $Office
    Title        = $Title
}
$Object.psobject.properties | ForEach-Object { $_.Value = 'N/A' }

The ouput is:

GivenName    : N/A
Surname      : N/A
EmailAddress : N/A
Office       : N/A
Title        : N/A
3 Likes

I can’t replicate that error message. Even if the object is not created, you don’t get that error message.

2 Likes