Welcome › Forums › General PowerShell Q&A › -ExcludeProperty with List of Properties in a Variable
- This topic has 1 reply, 1 voice, and was last updated 1 month ago by
Participant.
-
AuthorPosts
-
-
December 17, 2020 at 5:09 pm #280572
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? -
December 17, 2020 at 5:14 pm #280575
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?
-
-
AuthorPosts
- You must be logged in to reply to this topic.