0

I have a CSV file which looks like this:

  OldEmailAddress,NewEmailAddress
  [email protected],[email protected]

Power shell code to import the file:

 $UserInput = Import-csv "C:\Powershell\UserList.txt" -Delimiter ",";

I am going though a foreach loop and trying to write the values but it's print it out as an array in the powershell Window.

    foreach ($User in $UserInput) 
    {
     Write-Host $User.OldEmailAddress;
    }

The console doesn't display just that filed but the full array.

   @{[email protected]; [email protected]}

Why is this occurring?

4
  • 2
    Write-host is stringifying your object $user. You will need to use write-host $($user.oldemailaddress) so that your object property pair will be evaluated as an expression. Alternatively, don’t use write-host; use write-output or simply use $user.oldemailaddress alone. Commented May 28, 2020 at 18:35
  • I'm unable to reproduce this behavior, are you sure you're executing the exact code as presented here? Commented May 28, 2020 at 18:38
  • So it works if i just do Write-host $User.oldEmailAddress, but it does if I do something like Write-Host "Testing $User.OldEmailAddress Commented May 28, 2020 at 18:44
  • when you wrap a dot-referenced object [$Thing.Property] in double quotes, the part before the dot is expanded. the dot and the property name are NOT expanded. this is covered rather often since it is such an easy error to make. [grin] Commented May 28, 2020 at 19:48

1 Answer 1

1

As for this...

"Testing $User.OldEmailAddress

...to make it work, do it this way...

 $UserInput = Import-Csv 'D:\temp\myemaildata.csv' -Delimiter ','

 ForEach($User in $UserInput)
{$User.OldEmailAddress}
<#
# Results

[email protected]
#>

ForEach($User in $UserInput)
{$User.NewEmailAddress}
<#
# Results

[email protected]
#>

ForEach($User in $UserInput)
{"$User.OldEmailAddress"}
<#
# Results

@{[email protected]; [email protected]}.OldEmailAddress
#>

ForEach($User in $UserInput){"$($User.OldEmailAddress)"}
<#
# Results

[email protected]
#>

ForEach($User in $UserInput){"$($User.NewEmailAddress)"}
<#
# Results

[email protected]
#>

... no Write-Host needed since output to the screen is the PowerShell default unless you tell it otherwise. ;-}

Yet, why use that extra loop at all, for example:

Import-Csv 'D:\temp\myemaildata.csv' -Delimiter ','
<#
# Results

OldEmailAddress NewEmailAddress
--------------- ---------------
[email protected]   [email protected]
#>

Import-Csv 'D:\temp\myemaildata.csv' -Delimiter ',' | 
ForEach{$PSItem}

OldEmailAddress NewEmailAddress
--------------- ---------------
[email protected]   [email protected]


 Import-Csv 'D:\temp\myemaildata.csv' -Delimiter ',' | 
ForEach{$PSItem.OldEmailAddress}
<#
# Results

[email protected]
#>

 Import-Csv 'D:\temp\myemaildata.csv' -Delimiter ',' | 
ForEach{$PSItem.NewEmailAddress}

<#
# Results

[email protected]
#>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.