why cant I run powershell script from current directory

Sherzaad 0 Reputation points
2024-02-02T08:41:20.33+00:00

the following is part for a c# code that I am working on:

string powerShellScriptFilename = @"C:\Users\sherz\OneDrive\Downloads\enableTouchScreen.ps1";
/* Create process start info*/
ProcessStartInfo psi = new ProcessStartInfo
    {
        FileName = "powershell.exe", Arguments = $ "-NoProfile -ExecutionPolicy Bypass -Command \"{powerShellScriptFilename}\"", RedirectStandardInput = false, RedirectStandardOutput = false, RedirectStandardError = false, CreateNoWindow = true, UseShellExecute = true, /*need to be set to true for 'Verb' to function*/ Verb = "runas" /*run application as Administrator*/ /*WorkingDirectory = Directory.GetCurrentDirectory()};*/ /*Create the processProcess*/
        process = new Process
        {
            StartInfo = psi
        }; /*Start the process*/
        process.Start();

With this above the script run as expected. The issue is if I move to the script to the 'currentDirectory' (ie the Project debug folder) the script is not executed. I already tried updating 'powerShellScriptFilename' with the corresponding absolute path, script still worth execute. As the code works if I chose a different location it seem to me that there may be some restriction or filepath issue. My aim is to try and avoid hard coding the filepath to the script and I would like my program to pick its current directory to find the script and run it. How can I test/do that if it is not possible to do that from the debug folder?

Windows for business Windows Server User experience PowerShell
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2024-02-02T09:11:30.3033333+00:00

    The current directory is not always the executable's directory.

    Try to build the absolute path programmatically. For example:

    string powerShellScriptFilename = Path.Combine( Path.GetDirectoryName( Assembly.GetEntryAssembly( ).Location ), "enableTouchScreen.ps1" );
    
    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2024-02-02T14:50:37.54+00:00

    You have to dot source the script name. (Put "." in front of the file name"). Open a command prompt and cd to some temp directory. Copy and paste these commands. The first one fails, but the second one works.

    echo 1 + 1 > test.ps1
    powershell.exe -command test.ps1 
    powershell.exe -command .\test.ps1
    del test.ps1   
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-5.1


  3. Sherzaad 0 Reputation points
    2024-02-07T05:45:14.0833333+00:00

    I did some more experimenting and it seems that the filepath length is/was indeed the issue. I resolved my problem by generation the 'shortfilepath' from the absolute filepath. My code ran executed just fine after that! :)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.