PowerShell Script to Create PowerShell Profile

Posted by Brian Jackett on Geeks with Blogs See other posts from Geeks with Blogs or by Brian Jackett
Published on Mon, 29 Mar 2010 20:39:54 GMT Indexed on 2010/03/30 2:43 UTC
Read the original article Hit count: 1044

Filed under:

    Utilizing a PowerShell profile can help any PowerShell user save time getting up and running with their work.  For those unfamiliar a PowerShell profile is a file you can store any PowerShell commands that you want to run when you fire up a PowerShell console (or ISE.)  In my typical profiles (example here) I load assemblies (like SharePoint 2007 DLL), set aliases, set environment variable values (such as max history), and perform other general customizations to make my work easier.  Below is a sample script that will check to see if a PowerShell profile (Console or ISE) exists and create it if not found.  The .ps1 script file version can also be downloaded from my SkyDrive here.

Note: if downloading the .ps1 file, be sure you have enabled unsigned scripts to run on your machine as I have not signed mine.

 
$folderExists = test-path -path $Env:UserProfile\Documents\WindowsPowerShell
if($folderExists -eq $false)
{
    new-item -type directory -path $Env:UserProfile\Documents\WindowsPowerShell > $null
    echo "Containing folder for profile created at: $Env:UserProfile\Documents\WindowsPowerShell"
}
 
$profileExists = test-path -path $profile
if($profileExists -eq $false)
{
    new-item -type file -path $profile > $null
    echo "Profile file created at: $profile"
}

    A few things to note while going through the above script.

  • $Env:UserProfile represents the personal user folder (c:\documents and settings…. on older OSes like XP and c:\Users… on Win 7) so it adapts to whichever OS you are running but was tested against Windows 7 and Windows Server 2008 R2.
  • “ > $null” sends the command to a null stream.  Essentially this is equivalent to DOS scripting of “@ECHO OFF” by suppressing echoing the command just run, but only for the specific command it is appended to.  I haven’t yet found a better way to accomplish command suppression, but this is definitely not required for the script to work.
  • $profile represent a standard variable to the file path of the profile file.  It is dynamic based on whether you are running PowerShell Console or ISE.

 

Conclusion

    In less than two weeks (Apr. 10th to be exact) I’ll be heading down to SharePoint Saturday Charlotte (SPSCLT) to give two presentations on using PowerShell with SharePoint.  Since I’ll be prepping a lot of material for PowerShell I thought it only appropriate to pass along this nice little script I recently created.  If you’ve never used a PowerShell profile this is a great chance to start using one.  If you’ve been using a profile before, perhaps you learned a trick or two to add to your toolbox.  For those of you in the Charlotte, NC area sign up for the SharePoint Saturday and see some great content and community with great folks.

 

      -Frog Out

© Geeks with Blogs or respective owner