Question:
I have just downloaded the Register-TemporaryEvent
cmdlet from http://poshcode.org/2205 and placed it in my powershell profile directory near the $profile
script.
How do I create a new command Register-TemporaryEvent
which would be bound to this script?
Thanks.
Answer:
With PowerShell, you can execute scripts as commands if they are placed in directories contained in the ‘PATH’ environment variable. To see what directories are in the Path, you can use:
1 2 |
$env:Path -split ';'| sort |
You could modify the path permanently from Windows’ System Properties to include the location of your scripts, or you could temporarily modify the path from within your profile or script. In your particular case, you could add the following to your profile to add the profile directory to the path:
1 2 3 4 5 6 |
$ScriptRoot = Split-Path $SCRIPT:MyInvocation.MyCommand.Path if(($env:Path -split ';') -notcontains $ScriptRoot) { $env:Path += ';' + $ScriptRoot } |
You can then run the command as:
1 2 3 |
PS >$timer = New-Object Timers.Timer PS >Register-TemporaryEvent $timer Disposed { [Console]::Beep(100,100) } |
Note: When tab completing, it will complete as Register-TemporaryEvent.ps1
, but you can remove the ‘.ps1’ and it will still work.