Search PowerShellers and other PowerShell-related sites

Monday, March 26, 2007

Spring-cleaning of a PowerShell profile

After I read James Manning's post why share-able functions shouldn't be in your PowerShell profile, I have looked in my $profile. It was far from small file focused on prompt function, transcript function, one-liners and aliases. So, I created a few subfolders (AD, ISA, Exchange, Misc, Test...) for my scripts in WindowsPowerShell folder. Also, the functions found their place in dedicated folder Functions. The only thing that left is to put all folders with scripts in my path and to dot-source functions from Functions folder.


$profilehome = ([System.IO.FileInfo]$PROFILE).DirectoryName

dir $profilehome | where {$_.PsIsContainer -and ($_.name -ne "functions")} | %{$env:path += ";" + $profilehome + "\" + $_.name}

dir ($profilehome + "\functions") | where {!$_.PsIsContainer} |
%{. ($profilehome + "\functions\" + $_.name)}



$profile is now clean and tidy, and in James Manning's words - it's a far more robust, maintainable, shareable, and supportable situation than before.

Wednesday, March 14, 2007

Daily notes with PowerShell

The post at Holistic Detection blog gave me an idea how to keep my daily notes organized and just a few keystrokes away.


function New-DailyNotes {

# store files in subfolder notes in WindowsPowerShell folder
$path = (join-path ([System.IO.FileInfo]$profile).DirectoryName \notes\)
# files will be named like 2007-03-14_notes.txt
$notesfile = (get-date).ToString("yyyy-MM-dd") + "_notes.txt"

if( !(test-path $path ) )
{
# create the notes directory if it doesn't exist
new-item -path $path -type directory
}

$notespath = (join-path $path $notesfile)
if( !(test-path $notespath ) )
{
# create the notes file if it doesn't exist
new-item -path $path -name $notesfile -type "file"
notepad $notespath
}
else
{
notepad $notespath
}

}

set-alias dn New-DailyNotes



I've added this function to the profile file and I only have to type dn to get access to my daily notes file.