Wednesday, February 26, 2014

Mouseless Admin

Here’s some simple tips for driving Windows 8 from the command line, instead of the mouse.

Just search for it

Windows 7 has a built in search engine running by default. This can index your documents and emails and also Windows applications and utilities. You can usually find what you need by simply typing a keyword.

Pressing the Windows key or Start button will pop up menu with a box to type into. Windows 8 is similar: the Windows key (and Start button which was reintroduced for Windows 8.1) will open the Start screen. If you start typing it will automatically open the Search charm.

Command line

The traditional command line is still available. You can press Windows Key + R to open the Run dialog. Enter a command and hit Enter to execute it. This is great for simple single commands: see list below. If you want more interaction then you can use the Run dialog to open a command window by entering cmd, or a PowerShell window with powershell.

cmd Open a command window
powershell Open a powershell window
shutdown /s Shuts the computer down
shutdown /r Reboot
shutdown /l Logoff or signout
c:\ Opens a Windows Explorer window to the root of the C drive, for example. Works with any path.
%UserProfile% Opens a Windows Explorer window to your personal files

Switches

Most commands will take switches. In the list above I showed 3 switches that can be given to the shutdown command.

Most commands will take the /? switch which makes the command display some help. You’ll need to be in a command window to see the output.

Environment variables

The %UserProfile% command actually resolves an environment variable. In this case the variable, called UserProfile, contains the path to your personal file store area. When the Run dialog is given a path it opens it with Windows Explorer.

You can see the full list of environment variable by using the set command, but you’ll need to be in an interactive window to see the output.

Interactive commands

Here are some more commands, but these all require some interactivity to be useful.

set Displays current environment variables. Can also change them.
exit Quits the command line session and closes the window.
whoami Displays your username. Useful for verification when having permissions problems.

PowerShell

PowerShell is the command line’s big brother. You almost certainly want to use this in preference to the command line.

Elevating

Many administrative tasks require administrator rights, which are not available by default due to the User Account Control (UAC) security feature which was introduced in Windows Vista. You almost always want to leave UAC on. To do anything that requires administrator privileges simply elevate your PowerShell window, for example.

First launch a PowerShell window as described above. This won’t be elevated by default.

Now use that PowerShell to launch an elevated PowerShell:

Start-Process PowerShell –Verb RunAs

This will open another PowerShell window, this time with admin rights. Note the title of the window starts with Administrator: to indicate this.

Start-Process

You can save yourself a few keystrokes by using start instead of Start-Process. start is a PowerShell alias that simply maps back to the Start-Process commandlet.

You can use Start-Process to launch other applications, for example, and even URLs, which will open in your default browser:

Start-Process “http://google.co.uk”

File paths will open Windows Explorer and we can use built-in environment variables to refer to our home folder, for example:

Start-Process c:\users
Start-Process $home

You can use get-variable to list all the current environment variables.

You can also use Start-Process to switch to another account, which is useful if you typically run as a normal user, but have a special high-rights account for doing some tasks.

Start-Process PowerShell –Credential domain\hi-rights-user
Start-Process dsac

The first command launches PowerShell as another user. It will popup a window to ask for your password then open another PowerShell window. Use that for the second command, which launches the Active Directory Administrative Centre, for example and assuming you have it installed: search for Remote Server Admin Tools (RSAT) to get it.

No comments: