/* ]]> */
Mar 142012
 

Here’s a Powershell profile (startup script) that I use on several machines on a daily basis. If you use Powershell to any significant extent, I highly recommend customizing your environment to utilize a profile script.

I depend on this script SO MUCH! The script itself utilizes a script template that I developed which has some built-in features such as use of standard parameters -HELP (shows usage notes) and -HISTORY (which shows all of my maintenance notes). In addition, the script will automatically display a banner with some identifying information AND the most recent maintenance date. This maintenance date is important as that is what I use as a “version” identifier; if I start a Powershell session on a machine lacking the latest version, it will be evident by a quick glance to the displayed banner.

Ok, enough of that… let’s get on to what the script actually does…

Here are some of the features:

.ads in wordpress

  • Working Directory
    Sets a default working (scripts) directory (will create the directory if it is not already there)
  • Define Functions
    Defines a handful of frequently-used functions and sets short-cut aliases
    • list-profiles (alias: lpro)
      Display all of the possible profile locations and tells whether or not a file actually exists in each spot.
      $profile.AllUsersAllHosts
      $profile.AllUsersCurrentHost
      $profile.CurrentUserAllHosts
      $profile.CurrentUserCurrentHost
    • split-envpath (alias: epath)
      Splits all of the semi-colon delimited environment PATH items and presents in a list format.
    •  Get-LocalDisk (alias: gld)
      Get a quick human-readable view of your local disks. Details include DeviceId, Name, FreeSpace, and Capactiy. Disk details are presented in Gigabytes (much easier to read than default bytes view).
    • CountDown (alias: CntDn)
      This is the Countdown timer posted elsewhere on this site; I use this when doing some command-line adhoc query of a remote system that needs to repeat periodically; e.g. waiting for “something” to happen.
    • NewTimeStampedFile (alias: ntf)
      Also posted elsewhere on this site, I use NTF quite a bit where I need to write unique log files.
    • Credential Functions
      Folks, these two jewels are THE BOMB and fantastic for working with multiple accounts or background tasks where you need to specify credentials. The beauty here is that the credential are encrypted. And you are not just limited to Windows Domain or Host access! I use these functions to access my AWS (Amazon Web Services) S3 account data.
      • Export-Credentail (alias: ecred)
        Save credentials to a file
      • Import-Credential (alias: icred)
        Read the stored credentials file
    • get-command (alias: which)
      This is the Powershell built-in command; I’m just used to the UNIX “which” command which is very similar
  • Setup Aliases to executable programs
    This really saves me a LOT OF TIME. When I need to pull up a text editor or MS excel, I can do so quickly from the powershell command-line instead of mousing around for it. I’ve built a framework here that checks if a given program path is valid before creating an alias and it is structured so that adding more applications is a breeze. I have even built in support for x86/x64 system differences so that the script will search for the application’s presence in multiple locations before creating the alias.
    • Notepad++ (alias: npp)
      My favorite text editor!
    • Excel (alias: excel [how imaginative!])
      Great for csv files
    • Open Office Spreadsheet (alias: oo3)
      I use this on systems that I don’t want/need to license a copy of Excel (ok for small spreadsheets but really sucks if you’re an excel power-user)

So there you have it – if you find this half as useful as I do, it will be a keeper and used in your Powershell environment.

If you are brand-new to Powershell and do not already have a profile script, here’s a quick jumpstart for you:

Download the profile script on this site to your local machine (e.g. to c:\temp\myProfile.ps1)

You may have to “unblock” the script before powershell will actually let you use it. To do this, access the script via Windows Explorer, right-click on the file, select properties, click on the “unblock” button, and click “apply”.

open a powershell command window and do the following commands

if (!(test-path $profile.CurrentUserCurrentHost)){new-item $profile.CurrentUserCurrentHost -type file}

copy MyProfile.ps1 $profile.CurrentUserCurrentHost

set-executionpolicy unrestricted -force

Finally, close your PowerShell session and start up a new one.
You should see something similar to this:

Screenshot of Poshtips.com Profile Script

Screenshot of Poshtips.com Profile Script

Finally, here the script

Download Link : MyProfile.ps1 (right-click and select save as…)

Or cut-and-paste from the listing below

ENJOY!

 Posted by at 1:57 pm

  2 Responses to “New And Improved Powershell Profile (startup) Script”

  1. This has been really useful for me! Thank you for putting such a comprehensive description. Can I ask; is there a cmdlet that shows all the functions in a user profile, I see you entered the details manually using write-host? I cant seem to find one in help/online so i assume not? Thanks! Chris

  2. Hey Chris,
    Glad you find this to be useful. I’m not aware of any cmdlet that is capable of listing the functions defined in the user profile – that’s sort of what led me to list them out as part of the profile’s execution.
    Thanks for visiting!
    -xb90

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Page optimized by WP Minify WordPress Plugin