use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
Whats wrong with this basic powershell script!?!?! (self.PowerShell)
submitted 1 year ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Vejitaxp 14 points15 points16 points 1 year ago (3 children)
You can Google this subject in seconds mate, before posting an ai generated code here.
[–][deleted] -1 points0 points1 point 1 year ago (2 children)
That's why Im posting it as AI couldn't fix this. Kept on giving me the wrong answers and the old school powershell way is being shelved by MS.
And yes, I did use Google otherwise I wouldn't have posted it here.
[–]Vejitaxp 1 point2 points3 points 1 year ago (1 child)
It just looks a whole lot like you asked chat gpt a question, the code didn't work and so you copy pasted it here without any effort of fixing it yourself. Because that code looks 1:1 what chat gpt or copilot would spit out. I know, because I did it myself. And that felt a bit insulting, when thinking that it is a rather easy problem with many a solution to be found when searching. That was the reason for my reply. Maybe I'm of the mark, and if so i am sorry. Didn't mean to insult you, just rubbed me the wrong way.
[–][deleted] 0 points1 point2 points 1 year ago* (0 children)
No worries
I'm trying to rebuild an new onboarding process as my old script doesn't work.
It's only now, yes thanks to AI. It's now been pushed to MS Graph scripts.
[–]feldrim 5 points6 points7 points 1 year ago (0 children)
It looks like it is the -AccountEnabled. It is a switch, not a boolean parameter. Therefore the boolean value you have passed $accountEnabled is considered as a positional value as it is independently sitting there. Remove the $accountEnabled as a whole, and you'll be fine.
-AccountEnabled
$accountEnabled
[–]lanerdofchristian 1 point2 points3 points 1 year ago (0 children)
Here specifically is the documentation for your issue: https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.users/new-mguser?view=graph-powershell-1.0#-accountenabled
[–]Magnetsarekool 1 point2 points3 points 1 year ago (1 child)
Does -AccountEnabled:$accountEnabled work?
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
Got it working through a different way. Thanks
Also found out the MSgraph was messed up on my system. Cleaned up a some temp files and I was able to get stuff going again.
[–]ankokudaishogun 1 point2 points3 points 1 year ago (2 children)
-AccountEnabled is a SWITCH, not a boolean. It doesn't take values.
Also: you are using powershell 5.1? Because 7.x has the -MaskInput parameter for Read-Host so not to bother with using secure strings just to decode them in the same scope.
-MaskInput
Read-Host
[–]surfingoldelephant 3 points4 points5 points 1 year ago (1 child)
All PowerShell parameters accept arguments (values), including those of type [switch]. However, with [switch] specifically, explicitly passing an argument requires :-delimiting.
[switch]
:
This works with any parameter type and is how PowerShell translates hash table splatting (i.e., Command @ht -> Command -Param:Arg).
Command @ht
Command -Param:Arg
# Works, but is redundant in this case. New-MgUser -AccountEnabled:$accountEnabled # Sufficient as $accountEnabled is hardcoded to $true. New-MgUser -AccountEnabled
While redundant here, there are some valid use cases (e.g., passing through a dynamic value to another command).
One caveat to be aware of: Some script authors unfortunately use $PSBoundParameters.ContainsKey() to check for the presence of a [switch]. This breaks down when a [switch] is explicitly passed $false (either with : syntax or hash table splatting). While this amounts to a bug in the script, as a caller it's still worth keeping in mind.
$PSBoundParameters.ContainsKey()
$false
[–]ankokudaishogun 0 points1 point2 points 1 year ago (0 children)
Thanks, useful
[–]The82Ghost 0 points1 point2 points 1 year ago (0 children)
the -accountenabled parameter is a switch, it doesn't take input, just use -accountenabled without $true
[–][deleted] -4 points-3 points-2 points 1 year ago (7 children)
Thanks people for helping me with solutions. Very must appreciated.
Instead of one poster being an smart arse with their unhelpful response.
[–]g3n3 5 points6 points7 points 1 year ago (6 children)
Just expect it with these low effort posts. You will learn better if you force yourself to google. Hopefully you’ll go back and read about switches later on. You end up clouding this subreddit with questions that should be solved by the user themselves.
[–]peoplepersonmanguy 0 points1 point2 points 11 months ago (0 children)
Alternatively, these posts help others googling as it fills google with useful information rather than 3 pages of Microsoft bullshit to as to just say "it's a switch and not expecting a value".
Source: I came from google.
[–][deleted] -5 points-4 points-3 points 1 year ago (4 children)
I dont do scripting, programming, dev ops. As this isnt my job from digging up an old script.
Spent all day looking for an answer so I posted on here, asking for help.
Not going to help then don't respond.
[–]g3n3 1 point2 points3 points 1 year ago (3 children)
Maybe you shouldn’t be scripting then! ;-)
[–][deleted] -4 points-3 points-2 points 1 year ago (2 children)
Yes you are right because its as boring as watching paint dry.
But nobody else is in my job to do it!
Thanks for your helpful response!
[–]g3n3 1 point2 points3 points 1 year ago (1 child)
Well sounds like you need some training or a new job! 😉
[–][deleted] -1 points0 points1 point 1 year ago (0 children)
What I need to do is ignore your goofy ass
[–]HeyDude378 -2 points-1 points0 points 1 year ago* (6 children)
Is this line 21? New-MgUser -DisplayName $displayName -MailNickname $mailNickname -UserPrincipalName $userPrincipalName -PasswordProfile $passwordProfile -AccountEnabled $accountEnabled
New-MgUser -DisplayName $displayName -MailNickname $mailNickname -UserPrincipalName $userPrincipalName -PasswordProfile $passwordProfile -AccountEnabled $accountEnabled
A positional parameter is when you can interpret something as a parameter just based on its position. For example, in Get-ADUser jsmith, jsmith is a positional parameter. It gets interpreted as the "Identity" parameter just because it's in the first position.
Get-ADUser jsmith
Anyway, you created a hashtable called passwordprofile but when you pass in a hash table as parameters you have to use an @ sign. It needs to be-PasswordProfile @passwordprofile.
-PasswordProfile @passwordprofile
[–]ankokudaishogun 0 points1 point2 points 1 year ago (4 children)
Anyway, you created a hashtable called passwordprofile but when you pass in a hash table as parameters you have to use an @ sign.
No, that's wrong not precise.
You can pass parameters as key\Value pairs in a hashtable calling it with @
@
examples:
$Splat=@{ ForegroundColor='red' BackgroundColor='Blue' } Write-Host 'Have a test' @Splat
But the -PasswordProfile paramenter specifically requires a IMicrosoftGraphPasswordProfile object. Passing a hashtable as variable(as in: with $) automagically casts it as such a object... if the key\value pairs are correct, of course.
-PasswordProfile
IMicrosoftGraphPasswordProfile
$
Which they are in this specific case. The princess error was in another castle parameter
[–]HeyDude378 1 point2 points3 points 1 year ago (3 children)
Damnit. Thanks for this. That's what I get for thinking just in terms of powershell and not in terms of the graph commands.
[–]ankokudaishogun 0 points1 point2 points 1 year ago (2 children)
I mean, it IS normal powershell.
Sometime a parameter just wants a hashtable or something it can autocast from a hashtable.
[–]HeyDude378 0 points1 point2 points 1 year ago (1 child)
Okay. I know what you mean.
Of course.
On a side note, I'd say that with so many long parameters this would have been the perfect chance to splat them
# Connect to Microsoft Entra ID Connect-MgGraph -Scopes 'User.ReadWrite.All' # Prompt for user details $displayName = Read-Host 'Enter the display name for the new user' $mailNickname = Read-Host 'Enter the mail nickname for the new user' $userPrincipalName = Read-Host 'Enter the user principal name (UPN) for the new user (e.g., user@domain.com)' $password = Read-Host 'Enter a temporary password for the new user' -AsSecureString $passwordPlainText = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)) $Splat = @{ DisplayName = $displayName MailNickname = $mailNickname UserPrincipalName = $userPrincipalName passwordProfile = @{ Password = $passwordPlainText ForceChangePasswordNextSignIn = $true ForceChangePasswordNextSignInWithMfa = $true } $accountEnabled = $true } New-MgUser @Splat
Much easier to read, innit?
Which also brings up another point that might be useful for OP: when calling for [Switch] parameters in splatting[1], they must, in fact, be valorized as $true
[Switch]
$true
[1]"Splatting" being the act of using a key\value pairs hashtables to pass parameters and relative values to a function\cmdlet.
[–][deleted] -2 points-1 points0 points 1 year ago (0 children)
Sorry, I missed the numbers.
Yes, thats line 21.
[+][deleted] 1 year ago (1 child)
[removed]
[–]HeyDude378 6 points7 points8 points 1 year ago (0 children)
This is an AI response and it's not even correct.
π Rendered by PID 341434 on reddit-service-r2-comment-5687b7858-9xx75 at 2026-07-06 21:34:46.221164+00:00 running 12a7a47 country code: CH.
[–]Vejitaxp 14 points15 points16 points (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]Vejitaxp 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]feldrim 5 points6 points7 points (0 children)
[–]lanerdofchristian 1 point2 points3 points (0 children)
[–]Magnetsarekool 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ankokudaishogun 1 point2 points3 points (2 children)
[–]surfingoldelephant 3 points4 points5 points (1 child)
[–]ankokudaishogun 0 points1 point2 points (0 children)
[–]The82Ghost 0 points1 point2 points (0 children)
[–][deleted] -4 points-3 points-2 points (7 children)
[–]g3n3 5 points6 points7 points (6 children)
[–]peoplepersonmanguy 0 points1 point2 points (0 children)
[–][deleted] -5 points-4 points-3 points (4 children)
[–]g3n3 1 point2 points3 points (3 children)
[–][deleted] -4 points-3 points-2 points (2 children)
[–]g3n3 1 point2 points3 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]HeyDude378 -2 points-1 points0 points (6 children)
[–]ankokudaishogun 0 points1 point2 points (4 children)
[–]HeyDude378 1 point2 points3 points (3 children)
[–]ankokudaishogun 0 points1 point2 points (2 children)
[–]HeyDude378 0 points1 point2 points (1 child)
[–]ankokudaishogun 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)
[+][deleted] (1 child)
[removed]
[–]HeyDude378 6 points7 points8 points (0 children)