Shaun Luttin
Shaun Luttin

Reputation: 141512

Export functions from current session

I have created a function in my current PowerShell session. Now I want to save it for later use. How?

For instance, how can I use this in subsequent sessions?

function xfirefox {
    Start-Process firefox.exe $args
}

I know I just put it into my profile. I would like though, to be able to run something like Export-Function xfirefox myFunctions.txt instead.

Upvotes: 0

Views: 138

Answers (1)

Andy Arismendi
Andy Arismendi

Reputation: 52609

Perhaps put this in your profile. Remove -WhatIf when ready.

function say ($msg) {
    Write-Host $msg
}

function Export-Function ($Name) {
    $fx = (gcm -Name $Name -CommandType Function)
    if ($fx) {
@" 
function $($fx.Name) {
    $($fx.Definition)}
"@ | Out-File -FilePath $profile -Append -Encoding ascii -WhatIf
    }
}

Export-Function say

Upvotes: 2

Related Questions