Enhanced profile – Display my custom functions

Somehow, I can be compared to a goldfish : my memory is really, really poor… And I do eat fish! But no, I can’t help…
Sometimes I wrote a usefull function that I embed in my profile and some weeks after (ok, even the very next day) I forget about it… So, here’s my solution: list these functions in a MOTD way. Each time I open a PowerShell console, the list is displayed and my memory gets refreshed.

First, lets import all our custom functions. For better organization, I’ve put them in a module, called ‘FabFunctions’ (just a ‘FabFunctions.psm1’ file in a subdirectory ‘Modules’ of my WindowsPowershell home dir) 1:

Import-Module FabFunctions

Then, the function to list the custom functions (waouh, we’re next to a infinite loop here…)

function findFunctions ([string] $dir)
{
	$host.ui.RawUI.BackgroundColor="White"
	$parser = [System.Management.Automation.PsParser]
	$files=(dir $dir -Recurse *.ps*1)

	ForEach($file in $files) {
		$parser::Tokenize((Get-Content $file.FullName), [ref] $null) |
		ForEach {
		
			$PSToken=$_
			
			if($PSToken.Type -eq 'Keyword' -and $PSToken.Content -eq 'Function') 
			{
				$functionKeyWordFound=$true
			}

			if($functionKeyWordFound -and $PSToken.Type -eq 'CommandArgument') 
			{
				'' | Select @{Name="Added Functions";Expression={$PSToken.Content}				}
			  
				$functionKeyWordFound=$false
			}
		}
	}
	$host.ui.RawUI.BackgroundColor="Gray"
}

It has to be placed in $profile

Finally, still in our profile, we just have to call the functions and tell it where to find our module

findFunctions ../Modules/FabFunctions

Ok, and now, what does it look like?
ps_console

 

Note:
1 More on installing modules : http://msdn.microsoft.com/en-us/library/dd878350(v=vs.85).aspx

Leave a Reply

Scroll to Top