Am I an Administrator or not

Theo
Theo used Ask the Experts™
on
I want to edit my hosts-file (W10), but I am not allowed. When I check the perimissions of the file I have Full Control, but nevertheless I cannot save any changes. According to the User Accounts I am administrator but when I right click on the Command Prompt or the Power shell, to enter as an administrator, nothing happens.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Consultant
Commented:
These three lines of PowerShell tell you whether your current PowerShell session has administrative credentials

$identity  = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal( $identity )
$principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator )

Open in new window

It will probably return $false.

From a cmd.exe session enter
net localgroup administrators

Open in new window

If your username is in that list, you should be able to "run as administrator".

I just thought of this: if you should be able to run as administrator, then you can start a normal PowerShell session and enter this command:
start-process -filepath powershell.exe -arg "-noexit" -verb runas

Open in new window

That will start an elevated PowerShell session and you should be able to edit the file.
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Easy.  Right click on Notepad and select Run as Administrator. Navigate in Notepad to HOSTS. Edit and save. I do this whenever I have to edit HOSTS. Even if your are the Windows Admin, you still need to do this.

Author

Commented:
@ John: When I do this it tells me thet tha directory Etc does not contain any search results., whereas the Hosts file ought to be in here.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Once you have opened Notepad with Run as Administrator, navigate to the folder, and then for File Type (in Notepad), select All Files.  HOSTS will show up.
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Here you go.

Windows-10-Notepad-Admin-us
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
Hosts and other systems files are protected.  You must EXPLICITLY access them as an administrator... not just be logged in as one.  John has explained what to do and you can also run a command prompt as an admin and then navigate to c:\windows\system32\drivers\etc and then run notepad hosts to edit the file.
Top Expert 2016

Commented:
From Vista onwards we have the user access control technology to help secure our computer
Likewise in Linux if you are an administrator and you want to edit a protected item you would use
sudo command

In both cases an account that is an administrator will not be prompted for administrator credentials

When an administrator logs on, two separate access tokens are created for the user: a standard user access token and an administrator access token. The standard user access token contains the same user-specific information as the administrator access token, but the administrative Windows privileges and SIDs are removed. The standard user access token is used to start apps that do not perform administrative tasks (standard user apps). The standard user access token is then used to display the desktop (explorer.exe). Explorer.exe is the parent process from which all other user-initiated processes inherit their access token. As a result, all apps run as a standard user unless a user provides consent or credentials to approve an app to use a full administrative access token.

https://docs.microsoft.com/en-us/windows/security/identity-protection/user-account-control/how-user-account-control-works
Andrew LeniartIT Professional | Freelance Journalist | Looking for Opportunities
Distinguished Expert 2018

Commented:
I struck this issue myself recently but found an easy workaround that I used.

  1. Right click on the Hosts file in File Explorer, copy and paste the existing hosts file to something like hosts.txt
  2. Edit the new hosts.txt file
  3. Rename the existing hosts file to something like hosts.bak (Windows allows me to rename for some reason, but not over write it)
  4. Save the edited hosts.txt file and rename to just hosts (no extension)
  5. Done

You'll get some UAC prompts to confirm the above rename operation, but it does work.

I hope that's helpful.

Regards, Andrew
could possibly do this as well :

$file = "$env:windir\System32\drivers\etc\hosts"
"192.168.1.1 bob" | Add-Content -PassThru $file
"192.168.1.2 john" | Add-Content -PassThru $file


Script isn't hard.  But, they'd have to set or bypass their script execution policy and run it as administrator.
Might be better off to give them an old fashion batch file to get around the execution policy, but they'd still have to run as.


Set file="%windir%\System32\drivers\etc\hosts"
echo 192.168.1.1 bob >> %file%
echo 192.168.1.2 john >>  %file%

Found on Spiceworks: https://community.spiceworks.com/topic/1070436-update-hosts-file-with-powershell?utm_source=copy_paste&utm_campaign=growth

reference : https://community.spiceworks.com/topic/1070436-update-hosts-file-with-powershell
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Easier just to use Notepad as Administrator. Nothing else to do.

Author

Commented:
Gentlemen, thanks,
corowerIT Administrator

Commented:
as it has been mentioned - you still need to do rights elevation, even if logged in as admin.

if you have a regular need to edit - you can do it the following way -
1) create a shortcut to that file (i created it on desktop, but i use it REALLY often. you may choose your favorite location).
2) rightclick the shortcut and in properties preceed the filename by "notepad" (no quotes), and apply changes.
3) in advanced properties for shortcut tick "run as administrator" checkbox.

now when you invoke this shortcut, it immediately asks for access rights elevation, and - voila - save works as a charm.
also, you may just want to save a general shortcut for notepad (or whatever favorite plain text editor you have) with that "run as Administrator" checkbox, to be asked for access rights elevation immediately .

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial