0

I created a folder named Scripts in the c:\ drive, move my 2 scripts that were on my Desktop screen to this folder.

When I go to run any of the scripts the powershel screen opens and closes quickly and nothing is executed! On desktop they run normally. My PC user is administrator.

I saw instructions in the link below, but I preferred not to change the profile.ps1 file:
changing-execution-policy

Execution Policy command:

Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine     RemoteSigne

What do I have to do for my scripts to run from any folder on my PC?

I don't have powershell profile

PS C:\WINDOWS\system32> Test-Path $profile
false
PS C:\WINDOWS\system32>

To try to work around the problem i created a .bat file to run the script.

see in:
how-to-run-a-powershell-script-from-a-batch-file

But the command shows the error:

Command in .bat file:

PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile - ExecutionPolicy Bypass -File "%~dpn0.ps1'" -Verb RunAs}

Error message:

The string does not have the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

I changed the terminator ' in the command but I couldn't resolve the error! What is the correct syntax of the command?

4
  • You will have to edit your question so it includes more information about the differences between the two locations when it comes to your PowerShell profile.
    – Ramhound
    Commented May 12, 2022 at 13:23
  • @Clamarc are you certain it opens and closes quickly because of the execution policy? It may be the way the logic in the script it set to find other folders, etc. such as implicitly referenced file system objects rather than those being referenced explicitly. So if that is the case, that could be the problem and not execution policy. Show the logic to one of those scripts on pastebin, etc. Commented May 12, 2022 at 14:10
  • @Vomit IT - Chunky Mess Style is that script I sent you through pastebin about copying songs with 36 characters, remember?
    – Clamarc
    Commented May 12, 2022 at 16:03
  • @Clamarc - Questions shouldn't contain the solution to them within their body. The answer should contain that resolution. You also don't have to specify "Edited", we have a revision system for a reason, everyone can see what was added between each revision.
    – Ramhound
    Commented May 16, 2022 at 19:27

1 Answer 1

0

So it worked.

1 - The problem was not in the command, it was in the name of the folder where the files are.

Folder name:
Script's

Powershell interprets the single quote in the folder name as part of the command and gives an error when the command is executed. It was only necessary to change the name of the folder to Scripts, and the command worked.

2 - For the script to work running from another folder, you need:

1 - Create a .bat file, which will call the script;
2 - Place the .bat file and the script inside the same folder;
3 - The name of the .bat file and the script must be the same, eg.
    Myscript.bat
    Myscript.ps1

Command inside the .bat file:

echo off

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .