I have got a powershell script file which contains the function below. its called my_functions.ps1
function Get-My-PlainText()
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)][System.Security.SecureString]$SecureString
)
BEGIN { }
PROCESS
{
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString);
try
{
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);
}
finally
{
[Runtime.InteropServices.Marshal]::FreeBSTR($bstr);
}
}
END { }
}
I have got another script (called scriptB.ps1) that is supposed to call this function file before using the functions referenced by the file
scriptB.ps1
calls the function file by running the following inline within the file.
powershell .\my_functions.ps1
Import-Module .\my_functions.ps1
I get the error below.
The term 'Get-My-Plaintext' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is
| correct and try again.
Currently this behaviour happens on powershell terminal running powershell v 7.45, and the only way to fix it is to copy the function and paste it on the terminal.
Is there a way to resolve this problem and avoid the need to copy and paste the functions, its only this function that has this issue.
Thanks in advance.
There are 2 clear solutions to your issue.
1. dot sourcing
Change powershell to a dot
. .\my_functions.ps1
When you call powershell you are actually calling powershell.exe which launches a different process/session, loads the function into that session, then the session ends and takes that definition with it. Dot sourcing will pull that function into your current scope.
2. Rename .ps1 to .psm1 and use Import-Module
Import-Module .\my_functions.psm1
This will Import functions into the current session.
Also, as Darin pointed out in the comments, you have an extra pair of parenthesis. When you define a function, you can use the inline syntax
Function Get-Something ($Param1, $Param2){
...
}
or with a Param()
block. (Decorating the param block with [cmdletbinding()]
or at least one Parameter with [parameter()]
makes it an advanced function)
Function Get-Something {
[cmdletbinding()]
Param(
$Param1,
$Param2
)
...
}
It doesn't appear to cause issues as you have it, but is confusing for others at a minimum.
EDIT
mklement0 pointed out the Import-Module works on .ps1 as well. He is correct as I just confirmed. I'll leave the answer as is but renaming to psm1 is NOT required.