How to add a column to an existing array?

Welcome Forums General PowerShell Q&A How to add a column to an existing array?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
4 months, 1 week ago.

  • Author
    Posts
  • #108317

    Participant
    Points: 1
    Rank: Member

    I have an array named $ExpiredAdmin that I'm building withing a forEach loop.  I want to add column $property.SamAccountName to this existing array, but with a different column name?  Is this possible to do?

    clear-host
    $timestamp = (Get-Date -format "yyyy-MM-dd_HH-mm-ss")
    $path = (Get-Item -Path ".\" -Verbose).FullName

    $OutputFile = $path + "\"  + $timestamp + "_Cap_Locked_Expired.xlsx"
    Write-Host "OutputFile =" $OutputFile

    #$exists = Test-Path $OutputFile
    #If ($exists -like "True")
    #{Remove-Item $OutputFile else New-Item $OutputFile -ItemType File}
    Remove-Item $OutputFile -ErrorAction Ignore

    Import-module ActiveDirectory -ErrorAction stop

    $LockedOut = Search-ADAccount -UsersOnly -LockedOut |
    Get-ADUser -Properties DisplayName,
    Mail,
    LastlogonDate,
    Enabled,
    AccountLockoutTime,
    LastBadPasswordAttempt,
    BadPwdCount,
    LockedOut,
    Company,
    Description | Where-Object {$_.Company -like "*Cap*" -or $_.Description -like "*Cap*" -or $_.Description -like "*Local Admin Account*" }

    $LockedOut = $LockedOut | select SamAccountName, DisplayName, Company, Description, LockedOut, AccountLockoutTime, BadPwdCount, LastBadPasswordAttempt, Enabled, LastLogonDate, Mail

    # Get a list of expired user accounts
    $ExpiredUsers = Get-ADUser -filter {Company -like "*Cap*" -or Description -like "*Cap*"} -SearchBase "OU=Accounts,DC=mycompany,DC=com" -properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description | where {$_.Enabled -eq "True"} | where {$_.PasswordNeverExpires -eq $false} | where {$_.passwordexpired -eq $true}
    $ExpiredUsers = $ExpiredUsers | select SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description

    $ExpiredAdmWk = Get-ADUser -filter {SamAccountName -like "*-adm" -and SamAccountName -ne "svc-adm"} -SearchBase "OU=Accounts,DC=mycompany,DC=com" -properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description | where {$_.Enabled -eq "True"} | where {$_.PasswordNeverExpires -eq $false} | where {$_.passwordexpired -eq $true}
    $ctr = 0
    $ExpiredAdmin = @()
    foreach ($property in $ExpiredAdmWk)
    {
    # Get the Account Info for the non-admin account (SSO without the -adm)
    $ctr++
    #Write-Host $ctr $property.SamAccountName $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf("-adm"))
    try {
    $user = Get-ADUser -Identity $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf("-adm"))  -Properties SamAccountName, Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, Company, Description
    }
    catch
    {
    Write-Host "Error processing" $property.SamAccountName $property.SamAccountName.Substring(0,$property.SamAccountName.IndexOf("-adm"))
    }
    if ($user.Company -like "Cap*" -or $user.Description -like "Cap*")
    {
    $ExpiredAdmin += $user
    #Write-Host "** Cap User **" $property.SamAccountName $user.SamAccountName -ForegroundColor Yellow
    }
    }
    $ExpiredAdmin

  • #108359

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Please don't re-post repeatedly; your post was held for moderation, and the re-posting locked down your account.

    It'd be a bunch easier to help if you formatted your code ;). There are instructions immediately above the posting box, as well as a sticky note in every forum on how to format code. As-is, it's kind of hard to follow.

    Your arrays contain objects, which have properties; they're not "columns." The only way to add a property would be to either do so using Add-Member to each object prior to it going into the collection, or to enumerate the entire collection and add a property to each object one at a time (e.g., in a ForEach loop). For large collections this may be intensive, as .NET has to basically recreate the array behind the scenes for each object you modify.

    An alternative would be to pipe the collection of objects to Select-Object. You can then select all of the existing properties, and define a new custom property. There are examples of custom properties in the full help for Select-Object.

The topic ‘How to add a column to an existing array?’ is closed to new replies.