I'm having the same type of issue as the poster, but I'm not sure where to post it, or if I should make it a separate post altogether, since it's in the same category, so I'm posting it as an answer, in hopes of getting more clarification on this issue. If I need to post it somewhere else, please let me know where, and I'll change it, but please don't delete it, so I can just copy/paste it instead of having to rewrite what has taken me more than an hour to put together.
I'm running Windows 11 Home, most recent update was January 31, 2025. I'm trying to make sure I have access to Windows PowerShell for a program I'm taking in online school.
I have tried both ideas, and still keep getting told I have errors in my coding. The first code I was working on was
PS C:\Users\squea> (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
I tried this answer from above
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "You do not have Administrator rights to run this script.`nPlease re-run this script as an Administrator." Break }
And, I tried this answer from above
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "You do not have Administrator rights to run this script.
nPlease re-run this script as an Administrator." Break }
*squea is what the computer calls me (short for SqueakeyCat)
When I run the first code, instead of it coming up TRUE, like it says in my "getting started" (see below) it comes up FALSE.
To validate your environment, open an elevated PowerShell session and do the following:
Enter winver.exe and press enter to see the version details for your Windows device. Run $PSVersionTable.PSVersion. Verify your major version is at least 5, and your minor version at least 1. Learn more about
installing PowerShell on Windows.
Run the following command. The output shows True when you're a member of the built-in Administrators group.
So, I tried running it with the second part above added to it, and I get told the following
Unexpected token "if" in expression or statement.
- CategoryInfo: Parser error: (:) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId: MissingEndParenthesisInMethodCall
(that's the shortened version of it. The rest is at the beginning of this with what I had typed as the code about the "if" statement.)
Then, I tried it with the main part that I've been using, and the second suggestion above, and got the following message
At line:1 char:111
- ... Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole If(-Not (...
- Unexpected token "If" in expression or statement.
- CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId: UnexpectedToken
Now, I have PowerShell on my computer, and it has been enabled, but according to what I need to do, I need it to say TRUE, when running the first script, but it's coming back as FALSE instead.