chester89
chester89

Reputation: 8617

Basic PowerShell function not working

I have written this basic Powershell function that returns current UTC date and time:

Function UtcFileDate() {
    [System.DateTime]::UtcNow.ToString("yyyy-MM-ddTHH-mm-ss")
}

But when I run it the interpreter says:

The term is not recognized as the name of the cmdlet, function, script file, or operable program.

What am I missing here? What I do - put this function to a separate helpers.ps1 file, open PowerShell console, import the file via .\helpers.ps1, then call it like $x = UtcFileDate. Same thing on both Windows 7 and Win Server 2008.

Upvotes: 1

Views: 2066

Answers (1)

Lance U. Matthews
Lance U. Matthews

Reputation: 16596

When you run .\helpers.ps1 you are executing the script but your function will not be accessible from your session, hence the error. You need to dot source the script (see the section SCRIPT SCOPE AND DOT SOURCING of about_Scripts ) to make its functions, variables, etc. available to your running session:

. .\helpers.ps1
$x = UtcFileDate

Upvotes: 6

Related Questions