1

I am working with some scripts where I am calling some PowerShell (.ps1) scripts from batch files. I am unable to get this to work for this particular situation:

I have the following batch file:

@echo off
TITLE Modifying Quick Access Pinned Items
color f0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'r:\Tools\QuickAccessPin.ps1'";

QuickAccessPin.ps1:

.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Desktop"
.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Downloads"
.\Set-QuickAccess.ps1 -Action Unpin -Path "$env:userprofile\Documents"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Desktop"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Documents"
.\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Downloads"

Error When I Run the Batch File: (I inserted 'pause' at the end of the batch file to capture this; I get 8 of these, with this being the last one as indicated by the .ps1:8)

.\Set-QuickAccess.ps1 : The term '.\Set-QuickAccess.ps1' 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 R:\Tools\QuickAccessPin.ps1:7 char:1
+ .\Set-QuickAccess.ps1 -Action Pin -Path "$env:userprofile\Downloads
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\Set-QuickAccess.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I thought it might be a problem with my PS1 script, so I opened the file in the PowerShell ISE and pressed F5 to run the script. When I do that, the following happens and I have to click "Run Once" seven times - once for each of the commands called in QuickAccessPin.ps1

enter image description here

So, the PS1 is working, albeit not how it is supposed to, when I run it directly, but not working at all when I call it from the batch file. I should also point out that currently for testing, my Execution Policy is Unrestricted (usually RemoteSigned). What is more, to eliminate any problems, I also tried running the below so PowerShell does not prompt in the GUI to run the files:

@echo off
TITLE Unblocking PowerShell Tools
color f0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'r:\Tools\UnblockThisFolder.ps1'";
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force}"

UnblockThisFolder.ps1 is simply

Get-ChildItem r:\Tools | Unblock-File

...but that does not appear to do anything as PowerShell ISE still prompts me to run it, saying I can "unblock" the files if they are safe (which I have already done maybe 13 times now). If I set my policy to RemoteSigned and then run the script above, then Get-ExecutionPolicy again, it shows Unrestricted, so the 4th line works and the 3rd does not appear to do anything.

So the fact that it prompts me each time is only a minor annoyance, because this will all be automated and I will not be using any GUIs at all. But right now, I am unable to call the PS1 script from the batch file, which I need to be able to do, and the only way I can get my script to run is my doing it manually inside the ISE.

Does anyone know how I can fix this problem?

  • @McDonald's Unfortunately, that does not change anything. When I put PowerShell commands inside the quotes instead of another file, it works; I think it has something to do with calling the file - it is able to call the file, but then something snaps... – InterLinked Aug 31 '17 at 15:39
  • You need to start by doing a Set-Location to the path where the Set-QuickAccess.ps1 script is located. – Jeff Zeitlin Aug 31 '17 at 15:42
  • 1
    The error you're getting indicates that you're probably not in the directory you think you're in, and executing the scriptfile with the full path does not put you into that directory - so if Set-QuickAccess.ps1 is in R:\Tools, you're not guaranteed to be in R:\Tools when you execute the batch file. – Jeff Zeitlin Aug 31 '17 at 15:50
2

The error you’re getting indicates that you’re probably not in the directory you think you’re in, and executing the scriptfile with the full path does not put you into that directory - so if Set-QuickAccess.ps1 is in R:\Tools, you’re not guaranteed to be in R:\Tools when you execute the batch file.

You need to start by doing a Set-Location to the path where the Set-QuickAccess.ps1 script is located.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.