PowerShell Tricks - Trick 2

Posted in Powershell on May 4th, 2007 by nazeeh

So this one is quite a handy one. Do you ever find yourself wanting to jump back and forth between folders? I have quite a few folders on my system (or on the network) that I use everyday. I wanted to have a way to quickly jump to each of these folders regardless where I am. At the same time, I wanted someway to be able to create such jump points whenever I want to.

The solution I came up with was to create functions that would change my location to the target folder I want to jump to. So I had a function I called “source” that would run “cd c:\source\” for instance. Then I took it one step ahead and created a function called “aliasthis” that would create such “jump” functions automatically.

To do that, I created a script file I called “aliases.ps1″. In it I added the following function:

   1: function aliasthis($alias)
   2: {
   3:     $path = (Get-Location).path;
   4:     if ($path.substring(1,1) -eq ‘:’)
   5:     {
   6:         $drive = $path.substring(0,2);
   7:         $script = “$drive ; cd `”$path`“”;
   8:     }
   9:     else
  10:     {
  11:         $script = “cd `”$path`“”
  12:     }
  13:
  14:     $function = “function global:$alias { $script }”;
  15:     add-content $env:psconfig\\myscripts\\aliases.ps1 $function
  16:     new-item function:global:$alias -value “$script”;
  17: }

To use that function, you type “aliasthis <aliasname>” and it will create a function with the name you specified that will take you to the location you ran the function from. So if you run it like this:

PS:c:\source> aliasthis source

It will create a function called “source” that will always take you to c:\source\.
The function does two things actually, it creates the function you want in the current shell session so you can use it right away, then

it adds the text of that function to the end of the aliases.ps1 file so that next time you start the shell, the function is available. So at the end of my aliases.ps1 file, I have stuff like:

function global:desktop { C: ; cd “C:Users\nazeehe\Desktop” }

Last, I make a call to aliases.ps1 from my profile.ps1 script to make sure this file gets loaded with every instance of the shell:
. aliases.ps1

That’s it! Works like a charm. I have quite a bit of jump functions declared now and I can make my way to any place on my PC or the network with just a few keystrokes!

PowerShell Tricks - Trick 1

Posted in Powershell on May 3rd, 2007 by nazeeh

Man I love Powershell! Finally there’s a shell on Windows that actually kicks major ass! I decided to start a series of Powershell tricks that I’ve learned. This one here is the first one :)

This first one is a simple piece of code in my profile.ps1 file that detects whether the shell is running as admin or not. If the shell is running as admin, it gets a red background to make that very clear:

To do that, edit your profile.ps1 file (or create it in a folder called WindowsPowershell in your Documents folder). The code I use to detect admin user or not is:

$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin)
{
  (get-host).UI.RawUI.Backgroundcolor=“DarkRed”
  clear-host
}


This will cause the shell to have a DarkRed background if you’re admin, otherwise it will be the default blue color.

The $IsAdmin variable is then defined throughout the script, so you can use it for any other customization such as the prompt:

function prompt
{
    if ($IsAdmin)
    {
        Write-Host (“PS (Admin) “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Admin Shell: “ + $(get-location)
    }
    else
    {
        Write-Host (“PS “ + $pwd +“>”) -nonewline -foregroundcolor Green
        $host.ui.rawui.WindowTitle = “Windows Powershell: “ + $(get-location)
    }
    return ” “
}

That’s it! Have fun! More stuff to come soon.

Expose for Vista! VistaExpose

Posted in Windows Vista on May 3rd, 2007 by nazeeh

Alright…I’ll admit it… I am quite the fan of Mac OS X. I LOVE the Expose feature specially and how well it works. So when Vista came out, I set out to find something that can replicate that effect. I came across this C# application that a guy threw together in like one night that does a pretty decent job of imitating Expose. The guy posted his code in the forum thread on gamedev.net. His version supports live thumbnails as well, so it’s pretty damn cool.

What his version missed though was polish. It was an .exe that you had to run everytime you wanted the effect. So I modified it to become a tray icon application with support for hot corners. This way you can specify a corner on the screen where if your mouse goes there, the effect activates.

You can get my version from here. I included the source (which is not very sexy) incase someone wants to take this even further!

This is what it looks like:

Setting multiple IPs to one NIC in Windows

Posted in Technology Stuff on May 3rd, 2007 by nazeeh

This has always been one of my pet peeves with Windows. There is no way through the UI to set multiple IPs on the same network card if you set the card to DHCP. If you set it to static IPs, you can do it through the UI and add as many IPs as you want. Once you switch to DHCP, you can’t add any other IPs.

This is very useful if you only have one network card and want to have it connect to multiple networks. To do that in Windows XP (I haven’t verified if it works on Vista or not yet), you need to do the following:

Edit your registry using “regedit.exe” and find the following keys:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<interface>\IPAddress

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<interface>\SubnetMask

<interface> will be GUID that you should be able to figure out which one is your network card by looking at the settings under it.

IPAddress and SubnetMask are multistring values. When you have DHCP on, they will have 0.0.0.0 in them both. Just add a new line with the IP and subnet masks you want to use on the same NIC. Reboot…and you’re done. Enjoy :)

The page's WebCounter count says that you are visitor number Visitor Counter by Digits
FireStats icon Powered by FireStats