-ExcludeProperty with List of Properties in a Variable

Welcome Forums General PowerShell Q&A -ExcludeProperty with List of Properties in a Variable

Viewing 1 reply thread
  • Author
    Posts
    • #280572
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Hi,
      I need to exclude columns from a CSV file and then write it out to a new CSV file. This of course works:
      $NewCSV = Import-Csv $AFileFullPath | select-object * -ExcludeProperty user_id,first_name,last_name,email -Verbose
      $NewCSV | Export-Csv -NoTypeInformation -LiteralPath $FileFullPath -Verbose
      What I need to do is pass that list of columns as a parameter. I’m using a non-PowerShell application to pass parameters to the script. The problem is, it’s treating the parameter as single string. I’ve tried passing them as follows:
      @(‘user_id’,’first_name’,’last_name’,’email’)
      “‘user_id’,’first_name’,’last_name’,’email'”

      ‘user_id’,’first_name’,’last_name’,’email'</p>
      I know this works too:
      $ExcludeColumns = @(‘user_id’,’first_name’,’last_name’,’email’)
      I somehow need to pass in the columns like this:
      user_id,first_name,last_name,email
      To have something similar to this:
      $NewCSV = Import-Csv $FileFullPath | select-object * -ExcludeProperty $ExcludeColumns -Verbose
      $NewCSV | Export-Csv -NoTypeInformation -LiteralPath $FileFullPath -Verbose
      So that if any other columns need to be updated, I can just add it to the application without having to adjust the code. I was thinking splitting the variable at the commas and assigning to an array, but somehow loop through the array to exclude the columns but not sure if or how that could be done. Ideas?

    • #280575
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Hi,

      I need to exclude columns from a CSV file and then write it out to a new CSV file. This of course works:

      $NewCSV = Import-Csv $AFileFullPath | select-object * -ExcludeProperty user_id,first_name,last_name,email -Verbose

      $NewCSV | Export-Csv -NoTypeInformation -LiteralPath $FileFullPath -Verbose

      What I need to do is pass that list of columns as a parameter. I’m using a non-PowerShell application to pass parameters to the script. The problem is, it’s treating the parameter as single string. I’ve tried passing them as follows:

      @(‘user_id’,’first_name’,’last_name’,’email’)

      “‘user_id’,’first_name’,’last_name’,’email'”

      ‘user_id’,’first_name’,’last_name’,’email’

      I know this works too:

      $ExcludeColumns = @(‘user_id’,’first_name’,’last_name’,’email’)

      I somehow need to pass in the columns like this:

      user_id,first_name,last_name,email

      To have something similar to this:

      $NewCSV = Import-Csv $FileFullPath | select-object * -ExcludeProperty $ExcludeColumns -Verbose

      $NewCSV | Export-Csv -NoTypeInformation -LiteralPath $FileFullPath -Verbose

      So that if any other columns need to be updated, I can just add it to the application without having to adjust the code. I was thinking splitting the variable at the commas and assigning to an array, but somehow loop through the array to exclude the columns but not sure if or how that could be done. Ideas?

Viewing 1 reply thread
  • You must be logged in to reply to this topic.