Understanding this snippet of code

Welcome Forums General PowerShell Q&A Understanding this snippet of code

Viewing 1 reply thread
  • Author
    Posts
    • #207486
      Participant
      Topics: 12
      Replies: 11
      Points: 156
      Rank: Participant

      Hello All,

       

      I can see someone on the internet has written some code which following this

      $Username = $Account.Username

      Now I understand $username is a variable and it is a product of the variable $account however what does the .username mean ? what is the coder trying to achieve by adding a .extension (.username)

       

      For example he includes it in his loop but this .username is new to me.

       

      $Username = $Account.Username
      If (Get-ADUser -Filter {Name -eq $Username})

      {
      $account | Select Username,SamAccountName | Out-File C:\temp\exist.txt -append
      Write-Host “$Username already exists in Active Directory” -ForegroundColor RED
      }
      Else
      {
      Write-Host “I am sorry, $Username does not exist.”
      }

      }

       

       

      Thanks

       

    • #207492
      Moderator
      Topics: 3
      Replies: 216
      Points: 1,176
      Helping Hand
      Rank: Community Hero

      @fishandchips82, Request you start with PowerShell basics before you really jump in into any PowerShell scripting…

      Please go through this tutorial for better understanding, it is a best place to start with and still valid…

      https://channel9.msdn.com/Series/GetStartedPowerShell3

      And coming to your question, it is called dot notation, and used to represent the object properties. $Account is a AD object it seems and .Username is a property of it to fetch the username and store it to $Username.

      Thank you.

       

Viewing 1 reply thread
  • You must be logged in to reply to this topic.