Download & Info
Friday, February 26, 2010
NirCmd Elevate Switch
Download & Info
Download & Info
We’ve covered the excellent multi-purpose command-line tool NirCmd from Nirsoft several times before. This post talks about the new command-line switches added in the current version of NirCmd. Earlier this month, I sent a NirCmd feature request to Nir Sofer, asking to include an "elevate" switch, which should launch an application elevated (Windows Vista, Windows 7 & 2008.) Nir was kind enough to incorporate this feature in the immediate version update (v2.40). Later another update, to fix a bug I reported, v2.41 was released.
NirCmd v2.40 and higher includes the "elevate" and "elevatecmd" switches to run and execute commands with administrator rights under Windows 7/Vista/2008. Here are some useful uses of this all-in-one tool.
Of course, you’ll see the User Account Control elevation dialog when you use NirCmd with "elevate" or "elevatecmd" switches. There is no way bypassing that without disabling UAC or without relaxing UAC settings.
To launch an application, say Notepad as administrator, use the following shortcut/command from the Run dialog:
nircmd elevate notepad.exe
or by including the full path:
nircmd elevate c:\windows\system32\notepad.exe
If the path contains spaces, enclose the path within double-quotes. For example:
nircmd elevate "C:\Program Files\Windows NT\Accessories\wordpad.exe"
IMO, this is probably the most needed thing in Windows 7 & Windows Vista. Windows allows you to run an application elevated by right-clicking the shortcut or executable, and choosing "Run as Administrator". However, there is no option to pass a parameter to the elevated application.
Example #1 – Edit the HOSTS file
To create a shortcut to edit the HOSTS file (you’ll have to elevate the editor in order to write to HOSTS), you use this command:
nircmd elevate Notepad "C:\Windows\System32\Drivers\Etc\HOSTS"
Example #2 – Register or Unregister a DLL
Want to register/unregister a DLL or OCX file quickly, without having to open the Command Prompt (as Administrator) first? Use this command from Start/Run:
nircmd elevate regsvr32 c:\windows\system32\jscript.dll
Example #3 – Take Ownership of a File or Folder
To Take Ownership of a folder, use:
NirCmd elevate cmd /k takeown /f
/r /d y
Followed by ICacls to assign Permissions.
NirCmd elevate cmd /k icacls
/grant administrators:F /T
(Usage of TakeOwn and ICacls for files/folders are explained in our article How to Take Ownership of a file or folder using command-line in Windows 7 and Vista.)
Example #4 – Open an Elevated Command Prompt at a Specified Folder
When you open an elevated Command Prompt using Start menu or using the Command Prompt shortcut, the default start directory is C:\Windows\System32. You use the CD command to change to a different directory. Using NirCmd, you can open an elevated Command Prompt window mentioning the start directory (say D:\Test, in this example).
nircmd elevate cmd.exe /k cd /d "D:\Test"
As noted earlier, there is also an elevatecmd switch added to NirCmd. Note that this is different from the command-line switch elevate. The elevatecmd switch runs a NirCmd command (internal commands of NirCmd) with administrator rights.
Writing to the Registry using NirCmd
Writing to HKEY_LOCAL_MACHINE requires elevation, and you’ll have to use elevatecmd if you’re using NirCmd’s "regsetval" switch to write to a registry value.
NirCmd elevatecmd regsetval sz "HKLM\Software\Test" "Value1" "test-data"
This writes the data "test-data" under the registry value "Value1" in the above registry key.