Pipeline input ByPropertyName

Hi, I have created .csv file with all necessary columns which would be used as input for New-ADUser cmdlet (pipeline input ByPropertyName):

Import-Csv -Path d:\Users.csv | New-ADUser -AccountPassword (ConvertTo-SecureString -AsPlainText “temppassword01” -Force) -PassThru | Enable-ADAccount

How to use Write-Verbose to display info about every user being created? With For-Each loop it is easy but how to do that in this case?

You should just have to add -verbose to your New-ADUser cmdlet. Take a look at the help file about_commonparameters.

Ok, I know that but how to display some custom text e.g.:

Creating user John Smith
Creating user Ana Lane

There are a couple of issues here.

  • You should not have to enable the user. Creating a new user creates the user enabled.
  • Since you're using the same password for every user, create the SecureString outside the implied loop of the pipeline and assign it to a variable, e.g., $pw. Then use that variable in the pipeline. It doesn't make sense to call ConvertTo-SecureString over and over again to produce the same results.
It's going to be a lot easier to produce the verbose logging you want with a foreach loop as opposed to trying to jam it into a single pipeline. There's no shame in that. Just do it.