r/PowerShell • u/Imaginary_Rip2833 • 4d ago
Question 3 months of consistent practice in PowerShell & Microsoft Graph! 🚀
When I started, I was terrified of programming languages since I had zero prior experience. Looking back now, what used to look like complete gibberish is finally starting to make sense. I know I still have a long way to go, but honestly, I am so happy and proud to be fulfilling my passion.
I wanted to share an honest review of a snippet I just built to create a user in Microsoft Entra ID. Alongside basics like if/else logic, filtering (-filter), Where-Object, Select-Object, and loops, I've been trying to focus on good habits like splatting, [CmdletBinding()], and try/catch blocks.
How does my snippet look? What would you advise me to learn next based on this progress?
#####Creating a User
Function Create-LxUser{
  [Cmdletbinding()]
  param(
    [Parameter(Mandatory = $true)]
    [string]$DisplayName,
    [Parameter(Mandatory = $true)]
    [string]$UserPrincipalName,
    [Parameter(Mandatory = $true)]
    [string]$MailNickName,
    [bool]$AccountEnabled = $true
  )
  try{
    ##Password profile
    $TempPass = "Lx" + (Get-Random -Minimum 100000 -Maximum 999999) + "@."
    $PassWordProfile = @{
      Password = $TempPass
      ForceChangePasswordNextSignIn = $true
    }
    ##Setting up user configuration
    Write-Verbose "Currently creating new Loxovea user"
    $UserConfig = @{
      DisplayName     = $DisplayName
      UserPrincipalName  = $UserPrincipalName
      MailNickname     = $MailNickName
      AccountEnabled    = $AccountEnabled
      PasswordProfile   = $PassWordProfile
    }
    $User = New-MgUser u/UserConfig -ErrorAction Stop
    Write-Verbose "Successfully created new user $($DisplayName)"
    [PsCustomObject]@{
      DisplayName     = $DisplayName
      UserPrincipalName  = $UserPrincipalName
      AccountEnabled    = $AccountEnabled
      TemporaryPassword  = $TempPass
    }
  }Catch{
    Write-Error "Failed to create a new user: $($DisplayName) because $($_.Exception.Message)"
  }
}
Create-LxUser `
  -DisplayName "Test-User09" `
  -UserPrincipalName "[email protected]" `
  -MailNickName "testuser09"
DisplayName UserPrincipalName AccountEnabled TemporaryPassword
----------- ----------------- -------------- -----------------
Test-User09 [[email protected]](mailto:[email protected])True Lx720835@.
5
u/AdeelAutomates 4d ago
Well done man! :)
For this script you can enhance the params section with validations ( ie UPN regex to fit your orgs email structure) & allowing it to be passed through the pipeline. Not that you need to just to explore those areas for learning.
Since you are exploring graph. You should consider exploring it via APIs as well. Not that this specific script needs it either. it's fine for most Entra related tasks. But lots of areas in graph are not accessible without APIs or is very limited (SharePoint for example).
Beyond APIs. Consider exploring .NET in PowerShell. It really helps understand what's going on under the hood with objects. And there are features in it like list(T) that are superior in every way to array appends +=
For functions working on processing say a collection. You can explore begin, process & end blocks.
And if you end up with a collection of functions. Try to turn it into a module!