Mikey T.K. Mikey T.K. - 2 months ago 7x
PowerShell Question

Powershell: Forcing evaluation of a block of code in Add-member's -value option

I'm trying to import users into an active directory using a CSV file and a powershell script. I create a CSV with the headers normally associated with an AD object:


...and filled it up.

Now I want to use Powershell's
cmmdlet to generate users for each item in this sheet - the problem I'm having is that
requires a
, not just a normal string for an account's password. Skipping this conversion results in my users being created correctly, but with no passwords and their account disabled.

The command I'm using is as follows:

import-csv .\users.csv | add-member -passthru -memberType NoteProperty -value {$_ | select plaintextPassword | ConvertTo-SecureString -fromplaintext -force}

The result is user records like the following:

mail : tom.fubar@contoso.com
name : tom.fubar
givenName : Tom
middleName :
surname : Fubar
company : Contoso
department : IT
title : Technician
accountPassword : LongPasswordThatFitsADComplexityRequirements123!
path : OU=UserAccounts,OU=IT,OU=employees,DC=contoso,DC=com
description :
userPrincipalName : tom.fubar@contoso.com
encodedPassword : {$_ | select accountPassword | ConvertTo-SecureString -asplaintext -force}

The bit of code that should be evaluated for converting the plaintext password to a SecureString is being passed verbatim, rather than executed inline.

What is the proper way to force the code block to be evaluated, and use its result as the argument to
New-Member -value


  • Enclosing the script block in
    - Results in a null NoteProperty added to the object

  • Replacing the
    - Results in a null NoteProperty added to the object

(as shown by piping the whole command to


One solution I've found is to not bother with Add-Member, instead use a calculated property like so:

import-csv .\users.csv | 
    select -Property *, @{
        e={$_.plaintestPassword | ConvertTo-SecureString -fromplaintext -force}}

(Removed broken add-member after comment from @PetSerAl)