r/PowerShell • • Oct 02 '26

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 "testuser09@loxovea.com" `
    -MailNickName "testuser09"

DisplayName UserPrincipalName AccountEnabled TemporaryPassword

----------- ----------------- -------------- -----------------

Test-User09 testuser09@loxovea.comTrue Lx720835@.

47 Upvotes

34 comments sorted by

View all comments

3

u/eggeto Oct 02 '26

Always a 👌for People that use functions, Makes code more structured and easier to read

3

u/Imaginary_Rip2833 Oct 02 '26

​Appreciate it! Trying to build good habits from day one so I don't regret it when my automation scripts get ten times longer.

-3

u/hisae1421 Oct 02 '26

Meh it always bother me that I have to check what is in the function when it's called to understand what it does

1

u/Imaginary_Rip2833 Oct 02 '26

​"That's a super fair point! As someone new to this, where do you personally draw the line between modularizing code into functions versus keeping it inline for readability?"

2

u/eggeto Oct 02 '26

For anything you can reuse or incorporate into a larger whole,

using functions is recommended.

If you intend to apply CI/CD later on, it’s definitely advisable;

if you want to stick to small scripts, the choice is yours.
anyway the choice is always yours :-)

1

u/ForsakeTheEarth Oct 02 '26

what's with the quotes?

1

u/Celadin Oct 02 '26

Ha! If they'd said that without the quotes, in this day and age, it'd sound LLMish. I've found myself doing similar, couching language that might be seen as LLM-vomit. How warped we've become 🙃

2

u/ForsakeTheEarth Oct 02 '26

I actually went the other way because of the quotes, which is why I asked, but yeah I feel you - as a longtime emdash enthusiast, I'm constantly worried people think I'm responding to them via LLM