locked
Pass variable between select-object expression RRS feed

  • Question

  • Afternoon,

    I'm having some trouble passing a variable between 2 select expressions.  The first line in bold generates the variable called $pass:

    $csvfile = $csvfile | Select-Object -Property *, @{label = 'Firstname'; expression = {if (($_.name -split " ")[0] -notmatch "-") {($_.name -split " ")[0]; $pass= ($_.name -split " ")[0]} elseif (($_.name -split " ")[0] -match "-") {($_.name -split " ")[1]; $pass= ($_.name -split " ")[1]} else {($_.name -split " ")[0]; $pass = ($_.name -split " ")[0]} } }

    but it appears to be disposed of once the second line in bold is called:

    $csvfile = $csvfile | Select-Object -Property *, @{label = 'Lastname'; expression = {$ln = ($_.Name -split " " )[0..100]; $ln | where-object -Filterscript {$_ -notcontains $pass }}}

    I know $pass exists because the 

    write-host $pass

    between the lines echo's the value.  I've also tried wrapping the second line in a function and passing the $pass value but this breaks things.  Is there a simple way to do this?

    Function updateCSV { Param ($file) $file #Import the CSV $csvfile = import-csv $file #Split the Name to new fields Firstname & Lastname $csvfile = $csvfile | Select-Object -Property *, @{label = 'Firstname'; expression = {if (($_.name -split " ")[0] -notmatch "-") {($_.name -split " ")[0]; $pass= ($_.name -split " ")[0]} elseif (($_.name -split " ")[0] -match "-") {($_.name -split " ")[1]; $pass= ($_.name -split " ")[1]} else {($_.name -split " ")[0]; $pass = ($_.name -split " ")[0]} } } write-host $pass $csvfile = $csvfile | Select-Object -Property *, @{label = 'Lastname'; expression = {$ln = ($_.Name -split " " )[0..100]; $ln | where-object -Filterscript {$_ -notcontains $pass }}} $csvfile | Export-Csv -Path $file -NoTypeInformation } set-location "c:\scripts\CSVs" $GetFiles = get-childitem C:\scripts\CSVs -recurse | where {$_.extension -eq ".csv"} Foreach ($file in $GetFiles){ UpdateCSV $file }



    Alter De Ruine


    Thursday, October 19, 2017 3:56 PM

All replies

  • What are you trying to ask?  Your explanation is unclear and the code is very much like spaghetti.\

    Use a regex and alter the CSV directly.  It can be done in one pass.

    import-csv $file | 
    	Select *,FirstName,LastName |
    	ForEach-Object{
    		$_.FirstName = GetFirstName $_.Name
    		$_.LastName = GetLastName $_.Name
    	} |
    	Export-Csv file ...

    Now just create two functions to extract the names.  Leave the export of until you get the functions working correctly.


    \_(ツ)_/


    • Edited by jrv Thursday, October 19, 2017 4:13 PM
    Thursday, October 19, 2017 4:08 PM
  • OK so in essence I want to be able to exclude the array index number that is the variable in $pass 

    $ln = ($_.Name -split " " )[0..100]; $ln2 = $ln.where({$_.index -ne $pass});$ln2}

    So if $pass is the number 1 $ln should equal [0 + 2..10]

    I can't seem to get anything to work.  I've made $pass acsessible by making it a global variable


    Alter De Ruine

    Thursday, October 19, 2017 8:50 PM
  • To get first name from a full name you need to split it on the space.

    $firstname = ($.name -split ' ')[0]

    Why is that so hard?

    If your names  have multiple elements then lastname is:

    $lstname = ($.name -split ' ')[-1]


    \_(ツ)_/

    Thursday, October 19, 2017 9:45 PM