Powershell ps1 file “is not recognized as a cmdlet, function, operable program, or script file.”

前端 未结 3 2019
你的背包
你的背包 2020-12-11 14:36

I made a Powershell function just now and saved it to a ps1 file. However, when I try to execute it from within powershell, it won\'t run.

I\'ve allready changed to

3条回答
  •  悲&欢浪女
    2020-12-11 15:00

    If you replace "function listallpaths" with param and get rid of the surrounding {} like this..

    param([string]$fromFolder, [string]$filter, [string]$printfile)
    Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
    

    You will have a script file that you can call as required.

    PS> .\listAllPaths.ps1 c:\ *.pdf testingPDF.txt
    

    As Matt alluded to, by declaring the function, when you called the script, it would create the function and then exit. A PowerShell script is basically a function stored in a file (without the surrounding braces.. they are implied), where the function itself would be stored in memory.

提交回复
热议问题