Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Weekend Scripter: Exploring Powershell Profiles

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, explores Windows PowerShell profiles and locations.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about the weekend is that it provides two entire days with no meetings, no specific obligations, and it gives me time to think and reflect. This is also a time to play with Windows PowerShell.

Since Windows PowerShell 1.0 came out, the $profile automatic variable points to the CurrerentUserCurrentHost Windows PowerShell profile. This is extremely helpful because otherwise it would be a heck of a lot of typing, even when using tab expansion. Here is an example:

PS C:\> $PROFILE

C:\Users\ed\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PS C:\>

See what I mean? Dude. That is a lot of typing. But I do not have to type any of that, because $profile points to the location. The really cool thing is that it points to that location even if I do not have a profile. That is sweet!

But Windows PowerShell has more than one profile. In fact, there are six when one takes into account the Windows PowerShell console and the Windows PowerShell ISE. For more information, see Understanding the Six PowerShell Profiles. I have also written about the profiles, using them, and best practices for managing them. To read more about profiles, see these Hey, Scripting Guy! Blog posts.

Does a profile exist?

I ran into an issue the other day, in that Windows PowerShell was acting a bit funky. I looked in my Windows PowerShell console profile, but I did not see anything wrong. I thought, "Hmmmm. I must have added something somewhere else. But what is the easiest way to see if a profile exists?"

Well, it must be to list out the profile paths, and use Test-Path. If I use Get-Member to list the note properties, and use Force to show hidden properties, I can find the names of the profiles, in additon to the path.

The problem is that to get the path, I would have to use a regular expression to parse the string. This is because $profile returns a string, and the extra profiles are added in as note properties. This is shown here:

PS C:\> $PROFILE | gm -MemberType NoteProperty -Force

   TypeName: System.String

Name                   MemberType   Definition

----                   ----------   ----------

AllUsersAllHosts       NoteProperty System.String AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profil...

AllUsersCurrentHost    NoteProperty System.String AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Mic...

CurrentUserAllHosts    NoteProperty System.String CurrentUserAllHosts=C:\Users\ed\Documents\WindowsPowerShell\profil...

CurrentUserCurrentHost NoteProperty System.String CurrentUserCurrentHost=C:\Users\ed\Documents\WindowsPowerShell\Mic...

I do not like to parse strings with regular expressions if I don’t have to do so. And I know that I can find a specific path to a specific profile by adding a dot to the end of $profile and calling it by name. This is shown here:

PS C:\> $PROFILE.AllUsersAllHosts

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

Hmmm, so I need to store the profile names in an array, and walk through them after I append them to $profile. First I get the profile names and store them in an array, as shown here:

$p = $PROFILE | gm -MemberType NoteProperty -Force | % {$_.name}

I now check to see if it worked:

PS C:\> $p[0]

AllUsersAllHosts

Yep. So far so good. Now, I decide to walk through the array:

PS C:\> $p | foreach {$PROFILE.$_}

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

C:\Users\ed\Documents\WindowsPowerShell\profile.ps1

C:\Users\ed\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

OK. Now I need to find out which profiles exist. I use the Test-Path cmdlet to do this. Here is the command:

PS C:\> $p | foreach {$PROFILE.$_ ; Test-Path $profile.$_}

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

False

C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

False

C:\Users\ed\Documents\WindowsPowerShell\profile.ps1

False

C:\Users\ed\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

True

I can see that, in fact, I only have one profile for my Windows PowerShell console. There are still two other profiles that I need to check via the Windows PowerShell ISE. But because my issue was in the console, I need to go into the various modules that I use. Anyway, now I have a starting point. See you tomorrow.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles