Windows 10: Powershell doubt


  1. Posts : 382
    windows 10 (x64) Home 1803
       15 Jul 2018 #1

    Powershell doubt


    I have created a script in D:\Data\Lvg\gadgets\Laptops\Docs\powershell called myfirstfunction.ps1. I copied the same file as mysecfunc.ps1 in C:\. As you can see in following pic, .ps1 in C:\ works when I run it. But myfirstfunction.ps1 does not run either from C:\ or D:\Data\Lvg\gadgets\Laptops\Docs\powershell .
    Click image for larger version. 

Name:	PSDoubt.PNG 
Views:	35 
Size:	27.4 KB 
ID:	195877
    Any reason?
      My ComputerSystem Spec


  2. Posts : 931
    Windows 10 Home x64 and Pro x86
       15 Jul 2018 #2

    Do you mean it does not run or the PowerShell window opens and then closes?

    Your script looks like it's running in the PowerShell ISE UI that you show a diagram of but there is nothing to keep the window open after your script completes.

    There are lots of choices for this -
    1 Just add a final line pause just as you might with a batch file
    2 Add a PowerShell command to achieve the same thing - there are lots of choices

    For example, you could use this

    Code:
    Function Get-Hello ($param1)
    {
        Clear-Host
        Write-Output "Hello, $param1!"
        Write-Host "Press any key to close this PS window
        $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    Get-Hello("World")
    The reason you are seeing differences in behaviour, "As you can see in following pic, .ps1 in C:\ works when I run it", is that PowerShell ISE is running the script with that environment staying open [just as a command might run in a Command prompt without closing its window].

    By the way, PowerShell ISE will run the function correctly and your .ps1 will run everything correctly. The ISE does not like the ReadKey method so you'll see error responses only when testing within ISE. Using pause instead of both the write-host & $x lines does not suffer from this ISE problem.

    Denis
      My ComputerSystem Spec


  3. Posts : 931
    Windows 10 Home x64 and Pro x86
       15 Jul 2018 #3

    I can see that I missed part of your question.

    When, in the ISE, you can type in
    .\
    you should see a list of the files in the current folder so that you can select your desired .ps1 file.
    - If you do not see the list then the .ps1 file would seem to be in a different folder instead.
    - But the response in that case would be an error response telling you that it cannot recognise it.
    - I have just been playing around and I cannot recreate the exact symptoms shown in your diagram.
    - I suggest that you test the .ps1 with the ISE closed just to prove that it does run properly. Then you'll be able to focus on its interaction with the ISE as the main problem.

    Denis
      My ComputerSystem Spec


  4. Posts : 931
    Windows 10 Home x64 and Pro x86
       15 Jul 2018 #4

    This is a better pause function for PS to add in when you have solved the central ISE problem

    Code:
    Function PSPause ($message)
    {
        # Check if running Powershell ISE
        if ($psISE)
        {
            Add-Type -AssemblyName System.Windows.Forms
            [System.Windows.Forms.MessageBox]::Show("$message")
        }
        else
        {
            Write-Host "$message" -ForegroundColor Yellow
            $x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        }
    }
    PSPause("Press any key to finish")
    I copied it from https://stackoverflow.com/questions/...ey-to-continue

    Denis
      My ComputerSystem Spec


  5. Posts : 382
    windows 10 (x64) Home 1803
    Thread Starter
       15 Jul 2018 #5

    Try3 said: View Post
    This is a better pause function for PS to add in when you have solved the central ISE problem

    Code:
    Function PSPause ($message)
    {
        # Check if running Powershell ISE
        if ($psISE)
        {
            Add-Type -AssemblyName System.Windows.Forms
            [System.Windows.Forms.MessageBox]::Show("$message")
        }
        else
        {
            Write-Host "$message" -ForegroundColor Yellow
            $x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        }
    }
    PSPause("Press any key to finish")
    I copied it from https://stackoverflow.com/questions/...ey-to-continue

    Denis
    Thanks Try3.
    Though initially it was not working, when I restarted laptop, it started working in all places.
      My ComputerSystem Spec


  6. Posts : 931
    Windows 10 Home x64 and Pro x86
       15 Jul 2018 #6

    That's interesting.

    Last week I found that some PowerShell test scripts were rejected on one of my computers [it started to bring up a security challenge for local scripts even though the execution policy was RemoteSigned]. The next day I restarted that computer then, later on, I repeated the test scripts to make sure I had recorded the problem accurately. All the test scripts ran properly. I know of no reason why our PowerShell scripts should yield varying responses before / after reboots.

    Curious.

    Denis
      My ComputerSystem Spec


 

Related Threads
Solved PowerShell in Performance & Maintenance
I have a 64-bit laptop running Win 10 Home. I just notice in All apps, there is an entry of PowerShell and PowerShell (x86). I realize the former is for 64-bit and the latter is for 32-bit. Question : In my 64-bit laptop, when I open...
Hello, Usually, for test environments with a few machines I used to make NAT Networks on VirtualBox since version 4.3 when they introduced that option. Now, I'm trying to do something similar with VMWare. I noted that in VMWare (in the Virtual...
Clean installation of W10 Enterprise doubt in Installation and Upgrade
Hi all, I just acquired a Windows 10 Enterprise key and updated from Pro to Enterprise. System upgraded, installed new features, etc. My question is this one: next time I install Windows in a clean installation, should I use W10 Pro media, or...
Solved No doubt my biggest turndown - Logging in in Installation and Upgrade
Geez, it's my computer!! What's up with this idea that you MUST login to use it. I couldn't believe that it can't be turned off. I guarantee that will turn a lot of people off besides me. They need to put back a choice here. Having to enter a...
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 02:27.
Find Us