1

I am trying to load a PowerShell module that executes a custom cmdlet function but I can't get it to load... I've applied the solutions of several previous questions, but now I'm just going in circles. Here are the specs of what I have done so far and the specific error that returns. Note that as I am new to PowerShell and programming in general, it wouldn't surprise me that my problem isn't a file path issue but a logic error in my actual function:

  1. I created a profile function for a custom cmdlet that allows me to open project files from two different paths:

           function push-project( $project )
        {
            pushd ~/Documents/Visual Studio 2015/Projects/$project;
            pushd ~/Documents/GitHub/$project;
        }
    
    New-Alias -Name pp -Value push-project;
    
  2. I created a module by saving the function as ProfileFunctions.psm1 in the following directory:

    ~\Documents\WindowsPowerShell\Modules\ProfileFunctions\ProfileFunctions.psm1
  3. To invoke the function, per its syntax, I type in pp $projectName into the PS console window, but the error that returns is standard not recognized:

    pp : The term 'pp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    
    
    • pp MyFirstApp
    • ~~
    • CategoryInfo : ObjectNotFound: (pp:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException
Community
  • 1
  • 1
Kanapolis
  • 311
  • 6
  • 22
  • 2
    What happens if you `Import-Module ProfileFunctions` ? – Eris Aug 08 '15 at 20:46
  • Does it work if you use the regular function name? – Trondh Aug 08 '15 at 21:06
  • @Eris, I get a "no valid module file exists" error when I attempt to import. So I guess PS isn't even creating one in the first place. – Kanapolis Aug 08 '15 at 21:17
  • 1
    If your module exists, it will be listed when you run `Get-Module -ListAvailable`. That said, if your goal is simply to load that function into your powershell environment, I would rather just use the $profile (do you know what I mean)? Or do you have another reason to use a module? – manyways Aug 09 '15 at 09:47
  • @gowayward the module doesn't exist. I switched to the module method because I wasn't able to create custom profile functions in the first place. I originally followed the steps that BeefyCode provides on his website [here](http://www.beefycode.com/post/Stupid-PowerShell-Tricks-1.aspx). There's gotta be a convention I'm missing, but I don't see it. – Kanapolis Aug 09 '15 at 17:21
  • 1
    @ElliotEwert I use custom functions in my profile. So going a bit of topic here, but in case it helps - to create a profile (if you don't have one) you can run `New-Item -Type file $profile`, then e.g. `ise $profile` to edit that file. If you add your function and alias in there it should work. Powershell auto-loads the $profile every time you open Powershell. It might block you with the execution policy which you can set with `Set-ExecutionPolicy RemoteSigned`. Sorry if you know all this already. – manyways Aug 10 '15 at 15:37

2 Answers2

2

I copied and pasted your code into a Windows 8.1 machine I have here. It worked great (apart from the folder names not existing, since I don't have Visual Studio).

Things off the top of my head that might stop your module from working:

The file is actually called ProfileFunctions.psm1.ps1 or ProfileFunctions.psm1.txt or something.

The Modules folder is saved in someone else's documents folder, or the Public documents folder.

You've accidentally put a space in the folder name (it must be WindowsPowerShell, not Windows PowerShell).

Greenstone Walker
  • 1,090
  • 9
  • 8
  • Your guess is correct, the fact that the hidden file extension is '.psm1.ps1' part of the problem. As it turns out, once I corrected this, the solution @Jower is also applicable. Thank you. – Kanapolis Aug 10 '15 at 18:17
1

I Think your problem is that the Alias pp is not exported from the module.

You either define the alias outside the module, as supposed to or explicitly export it from the module using.

Export-ModuleMember -Function pushproject -Alias pp

Find more details in this article Unable to Create a Powershell Alias in a script Module

Community
  • 1
  • 1
Jower
  • 565
  • 2
  • 8