Can't add a NoteProperty on a PSCustomObject


I’m trying to add a noteproperty to a variable that I’ve created importing a CSV file.
And I can’t… Any help?

CSV file:



$Users = Import-Csv C:\Users\user\Desktop\users.csv -Delimiter “;”
$Users | ForEach-Object {
$.Name = (get-culture).TextInfo.ToTitleCase($.Name.tolower())
$.Title = (get-culture).TextInfo.ToTitleCase($.Title.tolower())
$Users | ForEach-Object {$_ | Add-Member NoteProperty ADuser (Get-ADUser -Filter {displayname -eq $_.Name} -Properties SamAccountName).SamAccountName}


Get-ADUser : Propriedade: ‘Nome’ não encontrado no objeto de tipo:
No linha:1 caractere:71

  • $Colaboradores | ForEach-Object {$_ | Add-Member NoteProperty ADuser
    (Get-ADUser …
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentExcep
    + FullyQualifiedErrorId : Propriedade: 'Nome' não encontrado no objeto de
   tipo: 'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirec

I forgot:


PS C:&gt; $Users | gm

TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition

Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Title NoteProperty System.String Title=Support Analyst
Name NoteProperty System.String Name=Thomas Jefferson

Try this:

$Users = Import-Csv C:\Users\user\Desktop\users.csv -Delimiter ";" |
Select @{Name="Name";Expression={(get-culture).TextInfo.ToTitleCase($_.Name.ToLower())}},
       @{Name="AdUser";Expression={Get-ADUser -Filter "Displayname -eq $((get-culture).TextInfo.ToTitleCase($_.Title.ToLower()))" | Select -ExpandProperty SamAccountName}}

Rob Simmers,

Thanks once again.
But something isn’t working ok.
When I change this part to:

@{Name=“AdUser”;Expression={Get-ADUser -Filter {Displayname -eq “THOMAS JEFFERSON”} | Select -ExpandProperty SamAccountName}}

it works…
When I use this:


Don’t work…

Trying this, gives me the same error…

$Users | ForEach-Object { $_.Name = (get-culture).TextInfo.ToTitleCase($_.Name.tolower()) $_.Title = (get-culture).TextInfo.ToTitleCase($_.Title.tolower()) $_.ADUsers = Get-ADUser -Filter {DisplayName -eq $_.Name} | select -ExpandProperty SamAccountName }


Get-ADUser : Propriedade: ‘Nome’ não encontrado no objeto de tipo: ‘System.Management.Automation.PSCustomObject’.
No linha:5 caractere:18

  • $.UsuarioNoAD = Get-ADUser -Filter {DisplayName -eq $.Nome} | select -ExpandPr …
  •              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:slight_smile: [Get-ADUser], ArgumentException
    • FullyQualifiedErrorId : Propriedade: ‘Nome’ não encontrado no objeto de tipo: ‘System.Management.Automation.PSCustomObject’.,Microsoft
$users = Import-Csv -Path users.csv -Delimiter ";"
$results = $users | ForEach-Object {
    $name = (get-culture).TextInfo.ToTitleCase($_.Name.tolower())
        Name = $name
        Title = (get-culture).TextInfo.ToTitleCase($_.Title.tolower())
        SamAccountName = (Get-ADUser -Filter "Name -eq ""$name""").SamAccountName
# Sample outputs - pick one or more, your choice
$results | Format-Table -AutoSize
$results | Out-GridView
$results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
$results | Out-File -FilePath .\foo.txt -Encoding ASCII
$results | Export-Clixml -Path .\foo.xml -Encoding ASCII

Bob McCoy,

Thank you very much!
Worked like a charm =]

Nice to have smart people guiding us who doesn’t understand PowerShell very well.

I’m always a little leery of folks who will create an object and then do a bunch of add-member’s to it. It doesn’t make sense and it’s not very efficient. It’s better to create a hash table and then create the object once. I typically use the [PSCustomObject] accelerator and collect all the objects in a collection, such as $results above. That gives me tremendous flexibility with what I want to do with the resulting objects on the tail end.

Best of luck as you become more PowerShell proficient. It’s all in the doing.