0

How can I easily add the input country to each line in the output csv file so that if I execute this with several countries for the country parameter I get the country info on each line?

param([string[]]$Country="Norway",[string]$file="ExUsersNO.csv")

Import-Module ActiveDirectory
foreach ($i in $Country)
{
    Get-ADUser -searchbase "ou=$i,ou=FMS,dc=xx,dc=xx,dc=com" -Filter * -properties msExchVersion,name,mail,DistinguishedName,whenCreated |
    Select-Object -Property name,mail,DistinguishedName,msExchVersion,whenCreated |
    Sort-Object name |
    Export-Csv -Path ".\$file" -Append -Encoding UTF8
}

1 Answer 1

2

Use a calculated property with your select statement:

select name,mail,DistinguishedName,msExchVersion,whenCreated,@{Name='Country';Expression={$i}}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, It worked like a charm! (But I had to change the "," to ";" from ...'Country',Expression... to ...'Country';Expression...
Sorry about that, typo on my part. If it solved your problem, please "accept" the answer by clicking the checkmark on the left

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.