Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Create All PowerShell Profile Scripts

Posted on March 28, 2014April 9, 2014

talkbubbleWhenever I train on PowerShell I inevitably get around to discussing PowerShell profile scripts. For those of you new to PowerShell, a profile script is where you put all the commands you want to run that will define your PowerShell session just the way you need it. You might load some snapins, create some PSDrives or define some variables. There are actually several profile scripts that can be run, depending on whether you are in the PowerShell console, the PowerShell ISE or some other PowerShell host.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The profile paths are hard-coded. You can see all of them by looking at the built-in $profile variable.

PS C:\> $profile | select *

AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps
                         1
CurrentUserAllHosts    : C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 74

You can also look at just $profile which will show you the script for the current user on the current host. If you look at this in the ISE you'll see some things are different.

ISE-Profiles

This variable is a bit tricky. Even though you can see properties here, if you pipe $profile to Get-Member all you get is a string definition. Here's another way you can discover these.

PS C:\> $profile.psobject.properties | format-table Name,Value -auto

Name                   Value
----                   -----
AllUsersAllHosts       C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 74

Although I don't really care about the length so let's filter it out.

PS C:\> $profile.psobject.properties | select -first 4 | format-table Name,Value -auto

Name                   Value
----                   -----
AllUsersAllHosts       C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Now for the fun part. That value is a path so I can use Test-Path to see if it exists. If not, I can create the profile and at least indicate that is isn't being used. Because most of these files don't exist by default and in fact the WindowsPowerShell folder doesn't exist under Documents until you create it, you might have to create the parent folder first. So I came up with a little script called Create-AllProfiles.ps1.

#requires -version 3.0

[cmdletbinding(SupportsShouldProcess=$True)]
Param()

$profile.psobject.properties.value[0..3] | where { -Not (Test-path $_)} |
foreach {
  #create the parent folder if it doesn't exist
  $parent = Split-Path $_
  if (-Not (Test-Path -path $parent)) {
    mkdir $parent
   }

  #create the profile with this text
  #the here string must be left justified
$txt=@"
#requires -version $($psversiontable.PSVersion)

<#
This profile is not used and intentionally left blank.
 $env:userdomain\$env:username
 $(Get-Date)
#>

"@ 

#write the text to the profile script
Write-Host "Creating $_" -ForegroundColor Green
$txt | Out-File -FilePath $_

}

The script takes advantage of a feature in PowerShell 3 that will automatically enumerate properties. In PowerShell 2.0 I would need to do this:

PS C:\> $profile.psobject.properties | select -ExpandProperty Value -first 4
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

But now in v3 and later I can do this:

PS C:\> $profile.psobject.properties.value[0..3]
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

The value property would show all of the values, including the length so because I'm getting an array, I can use a range of index numbers to select just the ones I want. In my script each value is tested and those that don't exist are passed on to Foreach-Object.

For each path, I first split it so I can test if the parent path exists. If not, it is created. Then I create some text that will be inserted into each new profile script. The script supports -WhatIf so you can see what it would do before you run it for real.

create-allprofiles

I end up with a profile script that looks like this:

create-allprofiles-2

Ideally, I would recommend you digitally sign the profile scripts, assuming you are using an AllSigned execution policy, so that no changes can be made to these scripts without your knowledge. Just don't forget to run this in the PowerShell ISE as well. If you have profiles already created, they will be ignored. And should you decide later to take advantage of any of the profile scripts, they are ready for you to edit.

Learn, enjoy and have a great weekend.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: Create All PowerShell Profile Scripts”

  1. Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #74 - Windows Management - TechCenter - Dell Community

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d