1

CSV sample:

CSV Sample

I have the below code, where I would like to overwrite the current value in the PreviousGroup field. I know that -append adds to the end of the column, but that's not what I want to do.

$UserGroup = read-host "Enter Group Name"
$csvFile = Import-Csv "C:\HomeFolder\Locations.csv"
if ([string]::IsNullOrEmpty($PreviousGroup)) {$PreviousGroup = ""}
else {$PreviousGroup = $csvFile | Select-Object $csvFile.PreviousGroup -Verbose}

$csvFile.PreviousGroup = $UserGroup
$csvFile | Export-Csv

Secondly, is it possible to link Dom*_Groups in the below code to the list on the CSV?

param([Parameter(Mandatory = $false)]
        [ValidateSet(*"list from csv"*)] [string]$Dom1_Groups)
param([Parameter(Mandatory = $false)]
        [ValidateSet(*"list from csv"*)] [string]$Dom2_Groups)
mklement0
  • 382,024
  • 64
  • 607
  • 775
Nuisance
  • 25
  • 4
  • CSV is a simple text file, and if there is a specific string to replace, just search and replace that. This is a very common thing with [tons of search-replace samples all over the web](https://duckduckgo.com/?q=%27powershell+search+and+replace+csv+text%27&t=h_&ia=web) and in the Powershell help files. – postanote Jan 01 '21 at 22:17
  • As for the _Secondly_ part of your question: Please focus on a _single_ question at time. – mklement0 Jan 01 '21 at 22:52

1 Answers1

1

$csvFile, as returned from Import-Csv, is an array of [pscustomobject] instances.

Therefore, assigning to the .PreviousGroup property of $csvFile in an attempt to assign to its elements' .PreviousGroup properties will not work: while it's understandable to attempt this, given that getting the elements' property values this way does work, via member-access enumeration, member-access enumeration by design only works for getting, not also for setting property values.

The simplest solution is to use the .ForEach() array method:

# Set the .PreviousGroup property of all elements of array $csvFile
# to the value of $UserGroup.
$csvFile.ForEach('PreviousGroup', $UserGroup)

Caveat: As of PowerShell 7.1, the above method of assigning property values unexpectedly fails if the input object happens to be a scalar (single object), which can happen if the CSV file happens to contain just one data row; see GitHub issue #14527.
An - inefficient - workaround is to use @(), the array-subexpression operator:
@($csvFile).ForEach('PreviousGroup', $UserGroup)
or to use a script block ({ ... }):
$csvFile.ForEach({ $_.PreviousGroup = $UserGroup })

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • i guess I should consider using a text file, like `New-Item -path $PSScriptRoot -name PreviousGroup.txt -type "file" -Value $UserGroup` & `Set-Content $PSScriptRoot\PreviousGroup.txt -Value $UserGroup` at the end of my function. – Nuisance Jan 02 '21 at 15:11
  • @Nuisance, I thought you wanted to update _CSV_ data, not plain text. Does the solution in this answer not address the scenario in your question and, if not, why? – mklement0 Jan 02 '21 at 15:24
  • yes I wanted to use a single .CSV. I'm opting for the.txt solution because it is something I've done before on another script and only needs a few lines of code. – Nuisance Jan 03 '21 at 21:03