Scott's answer will do what you asked, but Microsoft recommends that console applications display an "access denied" message rather than prompt for elevation.
From http://msdn.microsoft.com/en-us/library/bb756922.aspx:
A console application presents its output on the console window and not
with a separate user interface. If an application needs a full administrator
access token to run, then that application needs to be launched from
an elevated console window.
You must do the following for console applications:
Mark that your application “asInvoker”: You can do this by authoring the manifest of your application in which you set RequestedExecutionLevel == asInvoker. This setup allows callers from non-elevated contexts to create your process, which allows them to proceed to step 2.
Provide an error message if application is run without a full administrator access token: If the application is launched in a non-elevated console, your application should give a brief message and exit. The recommended message is: "Access Denied. Administrator permissions are needed to use the selected options. Use an administrator command prompt to complete these tasks."
The application should also return the error code ERROR_ELEVATION_REQUIRED upon failure to launch to facilitate scripting.
My C# code for this is below. It is tested on Windows XP (administrator -> ok, standard user -> denied) and Windows Server 2008 (elevated administrator -> ok, non-elevated administrator -> denied, standard user -> denied).
static int Main(string[] args)
{
if (!HasAdministratorPrivileges())
{
Console.Error.WriteLine("Access Denied. Administrator permissions are " +
"needed to use the selected options. Use an administrator command " +
"prompt to complete these tasks.");
return 740; // ERROR_ELEVATION_REQUIRED
}
...
return 0;
}
private static bool HasAdministratorPrivileges()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}