0

I am trying to streamline how I execute some scripts I wrote by setting up a function and alias to run them. I currently have functions to change my directory to where the scripts need to be run, but when I try to run the script itself I get the following error:

C:\Users\me\Desktop\BoB : The term 'C:\Users\me\Desktop\BoB' 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
+ C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\jteit\Desktop\BoB:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The function is:

function run-scanner { "& 'C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1'" | Invoke-Expression }

I've tried a few variations based on other answers I've found, but I keep getting the same error. I would prefer to not remove the space on the path because other scripts use it successfully. Running the script from the ISE gives me no problems.

Ideally I would like the function to also allow me to have the script run on the folders I would like without changing the working directory (each script works on a particular set of files that are in a static location but some of them use $PWD to get the folders in the location). For example in my $profile file I have this function: function go-to-temp {cd "C:\Users\me\Desktop\Bob Tools\To be Formatted\Temp"} which I run before I execute the above script. I would like them rolled into a single command without my working directory changing (which would render the go-to-temp function redundant.

What am I doing wrong?

0

2 Answers 2

4

There is no reason to run your script through Invoke-Expression.

Unless your script relies on $PWD, then you should be able to execute it with the call operator: &. As the other poster mentioned, you can use dot-sourcing (.) if you need the variables the script generates, but this will import all global objects (aliases, variables, functions) to your current scope. If it does rely on $PWD, you can utilize Start-Process with -WorkingDirectory to avoid changing where you're at.

function Start-Scanner {
    & "$HOME\Desktop\BoB Tools\folderScannerV0.4.ps1"
}

or

function Start-Scanner {
    $startArgs = @{
        FilePath         = "$PSHOME\powershell.exe"
        ArgumentList     = '-File', "`"$HOME\Desktop\BoB Tools\folderScannerV0.4.ps1`""
        WorkingDirectory = "$HOME\Desktop\BoB Tools"
        NoNewWindow      = $true
        Wait             = $true
    }
    Start-Process @startArgs
}
Sign up to request clarification or add additional context in comments.

10 Comments

This appears to get me 90% of the way there, but when I run the function from the ISE a power shell window briefly pops up with an error I can't read and the script isn't executed. The function as it exists in my profile is almost identical to what you have, just with some path corrections.
@jasonmadesomething That's probably an error from your script which goes beyond the scope of what you asked. Why are you using the ISE over just launching the console or something more modern like VSCode?
I don't have permission to use visual studio and when I try it from the powershell console it doesn't recognize the function at all... in fact it errors even when I enter & $profile
@EBGreen I believe I do. All my other functions (mostly cd shortcuts) work. It is at C:\Users\me\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
@jasonmadesomething As a side-note, VSCode is free (cost as well as open-source). I'd strongly recommend you engage mgmt as necessary to have access to it if you write powershell often.
|
2

You can just use dot-sourcing for this:

function run-scanner { . 'C:\Users\me\Desktop\BoB Tools\folderScannerV0.4.ps1' }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.