Help Using Add-Content to populare a .CSV file/

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Jay Jones Jay Jones 2 years, 3 months ago.

  • Author
    Posts
  • #18079
    Profile photo of Jay Jones
    Jay Jones
    Participant

    Hello,

    I am trying to use the Add-Content Cmdlet to add variables input via Read-Host to an existing .csv file. I can get it working to an extent. The problem i have is that the variables don't get appended to the correct column. Instead, each variable gets added to the first column in the .csv.

    Function Input-UserDetail {
    [CmdletBinding()]
    Param(
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$Username,
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$FirstName, 
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$Surname, 
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$DispName, 
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$Desc, 
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$Title, 
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$Depar,
    
    [Parameter(ValueFromPipelineByPropertyName = $true)][string]$SamName,
    
    [Parameter()][String]$Path = "C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv"
    
         )
    
    $Username = Read-Host "Enter Full name of new staff member (e.g. John Smith, Lady Grey, George Bush)"
    $FirstName = Read-Host "Enter First name of new staff member"
    $Surname = Read-Host "Enter Surname of new staff member"
    $DispName = $Username
    $Desc = Read-Host "Enter Job title of new staff member"
    $Title = Read-Host "Enter Title of new staff member (Mr, Mrs, Miss etc)"
    $Depar = Read-Host "Enter Department of new staff member"
    $SamName = Read-Host "Enter Username name of new staff member in the format first initial and surname (e.g jsmith, lgrey, gbush)"
    
    Get-testCSV
    ForEach-Object{
    $_.Username
    $_.FirstName
    $_.Surname
    $_.DispName
    $_.Title
    $_.Depar
    $_.SamName
                   }
    Add-Content -Path $Path -Value $Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName
    
                                }

    This is my latest attempt. I have also tried these methods:

     Add-Content -Path $Path.Username -Value $Username
    
    Where-Object $_.Username | Add-Content -Path $Path -Value $Username
    
    Add-Content -Path $Path | Where-Object {$_.Username } | Set-Content $Username
    
    -Path $Path | Where-Object {-Value 'Username'} | Input-UserDetail -Username $_.Username    

    They all get the same result as shown in the attatched .csv.

    Any help greatly appreciated!

  • #18086
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    To answer the question, -Value is expecting a string and you are passing an array. So, you need to convert the Array to a string:

    $Username = "jjohn"
    $FirstName = "jim"
    $Surname = "john"
    $DispName = "Jim John"
    $Desc = "A user"
    $Title = "Worker Bee"
    $Depar = "Marketing"
    $samName = "jjohn"
    
    $test = $Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName
    $test.GetType() #returns System.Array
    #you can use the $test variable and do a -join or something like this
    @($Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName) -join ","
    #or use string format...
    $test = ("{0},{1},{2},{3},{4},{5},{6},{7}" -f $Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName)
    $test.GetType() #returns System.String

    You might consider a more "Powershelly" way of accomplishing this task. Leverage a Powershell object like this:

    #Generate a sample CSV to play with
    Get-Service -Name "*ws*" | Select Name, Status | Export-CSV C:\Test\test.csv
    #Open an empty hash table object
    $csv = @()
    #Import an existing CSV
    $csv = Import-CSV "C:\Test\test.csv"
    #Append a new record to the object
    $csv += New-Object -TypeName PSObject -Property @{Name='AnotherService';Status="Exploded"}
    
    $csv 
    #Use Export-CSV to overwrite the existing CSV
    $csv | Export-CSV C:\Test\test.csv
    
  • #18087
    Profile photo of Jay Jones
    Jay Jones
    Participant

    Hi Rob,

    Thanks for your reply.

    I understand what i was doing wrong now, i am using the method you suggested below. I didn't realise -Value was expecting an array.

    $test = ("{0},{1},{2},{3},{4},{5},{6},{7}" -f $Username,$FirstName,$Surname,$DispName,$Desc,$Title,$Depar,$SamName)
    

    Thanks for all of the detailed comments and alternative ways of achieving the result, this is very helpful.

  • #18089
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    To be clear, -Value is expecting a STRING and you were passing an ARRAY. the $test variable is a String.Format that generates a STRING.

  • #18095
    Profile photo of Jay Jones
    Jay Jones
    Participant

    That's what i meant, Monday brain fart!

You must be logged in to reply to this topic.