Can’t get pipeline variable to evaluate

Welcome Forums General PowerShell Q&A Can’t get pipeline variable to evaluate

Viewing 2 reply threads
  • Author
    Posts
    • #263693
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Hi.

      I would like to write the SamAccountName property of an account into the mailNickname property.

      1 nice simple string…

      I can’t for the life of me work out how to get the final Set-Aduser cmdlet to work, it fails to expand the $_.SamAccountName parameter and I end up with mailNickname being equal to “.SamAccountName”

      Any ideas?

       

       

    • #263702
      Participant
      Topics: 1
      Replies: 85
      Points: 387
      Helping Hand
      Rank: Contributor

      $_ is the current object in a pipeline. The current object concept exists when piping to a command that evaluates a script block. You are accessing $_.SamAccountName outside of a script block. A simple workaround for you is to pipe into Foreach-Object:

      You should also not use quotes around $_.SamAccountName. This will cause $_ to be expanded and then .SamAccountName will be added to the end of the expanded string. Quotes in this fashion can only be used with simple variable references. Your options are to not use quotes at all or use quotes around a sub-expression “$($_.SamAccountName)”.

    • #264755
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Thank you very much. I knew it would be easy.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.