Create variable from CSV

后端 未结 3 2051
眼角桃花
眼角桃花 2020-12-12 02:59

I want to make variables from a particular column in a CSV.

CSV will have the following headers:

FolderName,FolderManager,RoleGroup,ManagerEmail
         


        
3条回答
  •  忘掉有多难
    2020-12-12 03:32

    In PowerShell, you can import a CSV file and get back custom objects. Below code snippet shows how to import a CSV to generate objects from it and then dot reference the properties on each object in a pipeline to create the new variables (your specific use case here).

    PS>cat .\dummy.csv                                                                                        
    "foldername","FolderManager","RoleGroup"                                                                  
    "Accounts","UserA","ManagerA"                                                                             
    "HR","UserB","ManagerB"                                                                                   
    
    PS>$objectsFromCSV = Import-CSV -Path .\dummy.csv                                                         
    
    PS>$objectsFromCSV | Foreach-Object -Process {New-Variable -Name $PSItem.FolderName }                     
    
    PS>Get-Variable -name Accounts                                                                            
    
    Name                           Value                                                                      
    ----                           -----                                                                      
    Accounts                                                                                                                                              
    PS>Get-Variable -name HR                                                                                  
    
    Name                           Value                                                                      
    ----                           -----                                                                      
    HR    
    

    `

提交回复
热议问题