• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tsukasa no Hibi

Cloudy Sky. Occasional Rain.

You are here: Home / Blog / Scripting “sudo” with PowerShell

Scripting “sudo” with PowerShell

2008-03-15 by tsukasa 3 Comments

Now that I finally have some time on my hands to get a little into Microsoft PowerShell again I decided to work on small things to remember what I learned once (aeons ago 😉 ).

And guess what, there was a practical command I always lack on Windows, it’s the great sudo command from the Unix/Linux world. It basically starts a program with elevated rights (just the rough idea).

Because doing it yourself is fine but looking how other people do it is just as nice I decided to consult Google and found Peter Provost’s great post for a PowerShell script that does exactly what I wanted.

The script launches a new process with elevated rights, only one thing was bugging me a little (yeah, I’m nitpicking now!): I had to provide the full path to the executable. Gee-wiz, how dare you!

Easy to fix with the addition of a few lines of code:

if([System.IO.File]::Exists("$(get-location)\$file"))
{
$file = "$(Get-Location)\$file";
}

Now I can just type i.e. “sudo MyApp.exe” or “sudo MyDir\MyApp.exe” when the application is in that very directory and it launches.

I really enjoy myself working with PowerShell again. Personally I think it’s a shame this great piece of software isn’t a default part of Windows Server 2008 just because it’s .net based (yes, that’s the reason, you can’t install .net on Server Core setups without kinks)…

Filed Under: Blog

Reader Interactions

Comments

  1. David Kuhl says

    2012-01-16 at 23:54

    Unfortunately this does not help with starting and stopping services, which is really what I want administrative rights to do. But thanks for the tips.

    Reply
  2. Tsukasa says

    2012-01-21 at 13:44

    Heya David,

    you can easily achieve that by simply starting the “net” tool with the proper arguments using the “runas” verb.

    Reply

Trackbacks

  1. sudo for Powershell | Tech Haus says:
    2010-05-25 at 17:52

    […] I began looking into options for the Linux equivalent of sudo and came upon these two pages: Link 1, Link 2. Both describe simplified ways of trying to launch an app from an elevated account […]

    Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Tags

.net AutoHotkey Bitcasa Blog C# Docker Fonts Fun Google Chrome Hardware How To Life Linux Mono Mplayer Music My NAS nVidia OBS OpenEdge OpenSSH Palaver Pangya! Portable App PowerShell Programming Quote RaiDrive Reminder Scripting Software Streaming Technology Tips twitter Video Virtualization VMware Web Windows Wine WordPress Wuala YouTube ZNC

Cool stuff

  • AdiIRC
  • Affinity Designer
  • Affinity Photo
  • AquaSnap
  • Arch Linux
  • Archive Team
  • ConEmu
  • Debian
  • Directory Opus
  • Far Manager
  • FileLocator Pro
  • Fluent Search
  • foobar2000
  • Forte Agent
  • IRCCloud
  • ISBoxer
  • Jetbrains Rider
  • Newsblur
  • OBS Studio
  • Obsidian.md
  • RaiDrive
  • Sublime
  • SyncBackPro
  • The Semware Editor
  • True Launch Bar
  • Vivaldi
  • Wavebox
  • WinHex
  • WinRAR
  • WinSCP
  • XYplorer
  • ZNC
  • Zoom Player Max

Semantic Web

  • Mastodon
  • Tsukasa no Hibi
  • Tsukasa no Hibi Sitemap

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Footer

About

Going since 2004, Tsukasa no Hibi is my personal blog about technology, media and sometimes society.

More about Tsukasa no Hibi

WordPress · Log in