Pipeline input ByPropertyName

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 1 year, 1 month ago.

  • Author
  • #29143
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic

    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?

  • #29144
    Profile photo of Peter Jurgens
    Peter Jurgens

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

  • #29145
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic

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

    Creating user John Smith
    Creating user Ana Lane

  • #29146
    Profile photo of Bob McCoy
    Bob McCoy

    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.

You must be logged in to reply to this topic.