Add columns to csv file

michael lustig 356 Reputation points
2021-02-03T08:06:47.94+00:00

Hello everyone

I am trying to add columns to existing file.

I am import id from csv file..and I want to add 3 columns from active directory.

Someone can help me?

Thanks
Michael

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,245 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 34,271 Reputation points Microsoft Vendor
    2021-02-03T10:07:13.83+00:00

    Hi,

    The Add-Member cmdlet can add properties to an object for you.
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-member?view=powershell-7.1

    It could be like this

    Import-CSV D:\id.csv |  
    ForEach-Object {  
      $obj = Get-ADUser -Filter "name -eq $($_.id)" -Properties column1,column2,column3  |Select-Object column1,column2,column3    
      $_ |   
      Add-Member -MemberType NoteProperty -Name column1 -Value $obj.column1 -PassThru |  
      Add-Member -MemberType NoteProperty -Name column2 -Value $obj.column2 -PassThru |  
      Add-Member -MemberType NoteProperty -Name column3 -Value $obj.column3 -PassThru  
    } |  
    Export-CSV D:\result.csv -NoTypeInformation  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments