29

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 the settings for running unsigned code by entering this command:

set-executionpolicy remotesigned

The function is this:

Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}

What it does is create a textfile in which all the path's to a certain file are listed.
I've put it directly under c:\ and named the file listAllPaths, same as the function.

When I enter the following command inside Powershell:

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

I get an error saying:

The term 'listAllPaths.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

I've tried several things and I honestly don't know how to get this to work? What I expect is for a file to be created on the given path, c:\ in this example. That file having the name testingPDF.txt and the contents being the generated this.

Can someone tell me what I'm forgetting here.

And no, Google doesn't answer everything. Tried that one allready. I wouldn't come and ask it here if I hadn't allready tried the online search-engines.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
KdgDev
  • 14,299
  • 46
  • 120
  • 156
  • Related post - [PowerShell The term is not recognized as cmdlet function script file or operable program](https://stackoverflow.com/q/24156451/465053) – RBT Mar 17 '21 at 06:36

4 Answers4

14

This is a typical error across many platforms where your environment path does not include your current directory. so when you execute your script (or command or program etc), your runtime environment looks everywhere except your current/working directory.

Try

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

EDIT: After reading your comments, I'm going to suggest you try this. I haven't actually verified the logic of your PS script. I'm merely trying to get your script to execute first.

Try editing your script as below, and execute as above.

Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}

listAllPaths
Chaitan
  • 266
  • 1
  • 3
  • 8
  • Thanks, but it didn't work. I entered that command and it just jumped right over it. No error reported, just a new line in powershell for me to enter commands on. – KdgDev Apr 18 '09 at 05:03
  • Ok, so that means the ps script is actually executing, but no output is returned. Try to put some print statements to verify this. Now are you sure you didn't make another trivial mistake? I see you put your code in a function, but are you actually calling the Function in your script? – Chaitan Apr 18 '09 at 05:08
  • Calling the function in my script? I'm not that experienced with Powershell, what I posted here is the exact content of my script, no more, no less. – KdgDev Apr 18 '09 at 05:09
  • Added the line you sudgested, executed again the way you showed before, same result, just hops over it... – KdgDev Apr 18 '09 at 05:23
  • I tried Matt's sudgestion. I can now use the function in powershell as if it's native. Though it's still no very usefull if I move around a lot, since I'd have to define the function again and again on each machine... – KdgDev Apr 18 '09 at 05:31
  • Change the last line to this: "listAllPaths $args[0] $args[1] $args[2]" The way it is now, all of the arguments to your function are null values. – dan-gph Apr 18 '09 at 05:32
  • That's a lot of work to make the function run like that.. check out my answer below to get rid of some of that brittle overhead (the $args part). – Steven Murawski Apr 18 '09 at 13:52
7

I could be off base here, but is it that your script is defining a function, rather than executing it? Perhaps you need to "source" the script:

. .\listallpaths.ps1

... so that now your "listallpaths" function is defined.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • 1
    yeah, I can now simply typ the function and give parameters. Still, suppose I move around a lot and work with different PC/laptop's. Even if all of them have powershell, i'd have to redefine the function each time. What I really want to do in that case is just call the ps1 file and give it the arguments and have it do the work. – KdgDev Apr 18 '09 at 05:29
  • Matt - you are on the right track.. I just listed the modification to the script to get the runtime behavior WebDevHobo was looking for. – Steven Murawski Apr 18 '09 at 13:50
4

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.

Steven Murawski
  • 10,959
  • 41
  • 53
-1

Another reason this can happen is if you are running a couple scripts in succession, by relative path, and one of them uses cd or Set-Location to move you from where you think you are.

Example:

You start in $HOME, which is your home folder: C:\Users\YOU

Here you have the following scripts:

  • script_a.ps1
cd C:\
mkdir myfolder
  • script_b.ps1
mkdir myfolder2

and you try to run them in succession, like so:

. '.\script_a.ps1'
. '.\script_b.ps1'

You will see the error ".\script_b.ps1" is not recognized as the name of a cmdlet because there is no script_b in C:\ !

daevski
  • 101
  • 3
  • 9
  • That's not what's happening in the question, though. The author was invoking one script containing one function that does not call `Set-Location`. – Lance U. Matthews Apr 01 '22 at 01:22
  • True, but for others searching the internet for solutions, I thought this might be helpful. – daevski Apr 01 '22 at 15:03
  • That's not how Stack Overflow works, though. Answers should be [tailored _to the question_](https://stackoverflow.com/help/how-to-answer) and not some mystery future reader who might wander in from a search engine with a different problem. If one just read the title then, yes, this might seem like a reasonable answer, but the body of the question is there for a reason; you can't just ignore it. (Well, you _can_, but don't be surprised if you get downvoted for it, especially when, in doing so, you've bumped a question that was asked, answered, and became dormant **13 years ago**.) – Lance U. Matthews Apr 01 '22 at 21:06
  • Okay, thanks for the info. I'll take my downvotes. – daevski Apr 06 '22 at 00:11