Page 2 of 3 FirstFirst 123 LastLast
  1.    06 May 2017 #11
    Join Date : Feb 2017
    Home
    Posts : 480
    Windows 10 Home x64, V1709 (16299.64)
    Thread Starter

    Quote Originally Posted by Pyprohly View Post
    CWGilley, could you elaborate?

    If I have a folder “C:\Utilities” in the path variable with batch and powershell scripts in it, I can specify just the name of the batch or powershell script in a powershell console and it will run. Is this what isn’t working for you?
    That's right. None of the C:\Utilities do anything when I try to run them in PS. Here's what I get with $Archive.bat file:

    Windows PowerShell
    Copyright (C) 2016 Microsoft Corporation. All rights reserved.

    PS C:\Users\Clayton> cd\
    PS C:\> $Archive
    PS C:\>


    Even manually taking it to C:\ before the command gets nothing.

    gpglang, thanks for the link. I'm headed over there now.
      My ComputersSystem Spec
  2.    06 May 2017 #12
    Join Date : Mar 2015
    Sydney, NSW
    Posts : 93
    Windows 10

    Quote Originally Posted by CWGilley View Post
    Here's what I get with $Archive.bat file:
    ...
    PS C:\> $Archive
    Well there’s your problem. The dollar symbol is used to denote a variable in PowerShell.

    If you want to run that batch file with that special symbol in it, you’ll need to explicitly tell PowerShell you want to invoke it, so quote it and prepend an ampersand (the call operator).
    Code:
    PS> & '$Archive'
      My ComputerSystem Spec
  3.    06 May 2017 #13
    Join Date : Feb 2017
    Home
    Posts : 480
    Windows 10 Home x64, V1709 (16299.64)
    Thread Starter

    OK, bad example. Here's the simplest (and oldest) one I have named X.bat

    @ECHO Off
    CD\
    C:
    CLS
    Exit


    And it doesn't work either. Your code example does run. Thanks for that one.

    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
      My ComputersSystem Spec
  4.    06 May 2017 #14
    Join Date : Mar 2015
    Sydney, NSW
    Posts : 93
    Windows 10

    X.bat should clear the console screen, like cls, and that’s all it will do. It will not change the current directory because .bat files will be executed as a sub process in cmd.exe (as the %cmdcmdline% batch variable will reveal to you), if that’s what you mean by “doesn’t work”.

    Quote Originally Posted by CWGilley View Post
    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
    It’s just another program, really.
      My ComputerSystem Spec
  5.    06 May 2017 #15
    Join Date : Feb 2017
    Home
    Posts : 480
    Windows 10 Home x64, V1709 (16299.64)
    Thread Starter

    I had expected X.bat to "exit" the shell and return me to the desktop like it does when ran in Command Prompt. But now, thanks to your tutelage I have a much better understanding what PS is & does.

    Thank you for your time. It is much appreciated. Enjoy your weekend.
      My ComputersSystem Spec
  6.    06 May 2017 #16
    Join Date : Sep 2014
    Nashville, TN
    Posts : 3,143
    Windows 10 Pro

    Quote Originally Posted by CWGilley View Post
    OK, bad example. Here's the simplest (and oldest) one I have named X.bat

    @ECHO Off
    CD\
    C:
    CLS
    Exit


    And it doesn't work either. Your code example does run. Thanks for that one.

    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
    Not sure what you mean by "doesn't work". Does it execute and give you different results than you expect? Or does it not execute at all and give an error message?

    You need to be aware that batch files will spawn a separate CMD process and run, which means it will execute the above statements, but the Exit statement will exit the CMD process, leaving you back at the powershell process.

    To be frank, I'm not even sure why you would have such a batch script, since even in a normal command prompt all those actions will be performed and then the command prompt goes away.. you could have just aliased X to Exit and it would do the same thing, the stuff that comes before it are pointless.
      My ComputerSystem Spec
  7.    06 May 2017 #17
    Join Date : Feb 2017
    Home
    Posts : 480
    Windows 10 Home x64, V1709 (16299.64)
    Thread Starter

    They do not execute, no error message or anything else. The X.bat was created because typing "x" was shorter than "exit" to leave command prompt.

    No big deal and certainly not an "issue" or problem.
      My ComputersSystem Spec
  8.    06 May 2017 #18
    Join Date : Oct 2013
    A Finnish expat in Germany
    Posts : 12,971
    Windows 10 Pro

    To read PATH variable in PowerShell:
    $env:path
    (#1 in screenshot.)

    -- OR --

    Read it directly from registry:
    Get-ItemProperty 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH
    (#2 in screenshot.)

    Click image for larger version. 

Name:	2017-05-06_19h15_42.png 
Views:	4 
Size:	71.4 KB 
ID:	133816
    (Click to enlarge.)

    To add a folder in PATH, we need first read current path from registry to a variable (in this example I will use variable $CurrentPath, you can use any variable name you prefer), then write $CurrentPath plus folder we want to add to path to another variable (I will now use $ModifiedPath), and finally replace path in registry with modified path.

    Read current path to variable $CurrentPath:
    $CurrentPath=(Get-ItemProperty 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

    Add a folder (D:\MyScripts in this example) to path, write new path to variable $ModifiedPath:
    $ModifiedPath=$CurrentPath+';D:\MyScripts'

    Write new path to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
    Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $ModifiedPath

    Click image for larger version. 

Name:	2017-05-06_19h26_44.png 
Views:	3 
Size:	24.3 KB 
ID:	133815

    Notice that PowerShell does not show new path yet. You need to close PS and open a new session to see changes.

    Kari
      My ComputerSystem Spec
  9.    06 May 2017 #19
    Join Date : Sep 2014
    Nashville, TN
    Posts : 3,143
    Windows 10 Pro

    Quote Originally Posted by CWGilley View Post
    They do not execute, no error message or anything else. The X.bat was created because typing "x" was shorter than "exit" to leave command prompt.

    No big deal and certainly not an "issue" or problem.
    Yeah, and using an alias for Exit is also just typing a single X.

    Powershell will give you an error if it's not executing, so in fact, it did execute, you just didn't see anything (because of the @ECHO OFF). You can see this by altering your batch file to remove the Exit, and you will see you are still in a CMD shell, and not in a Powershell (there won't be a PS at the beginning). Then type Exit and you will see it returns to the PS prompt.

    Another tell-tale sign is to do a DIR in the CMD prompt vs in Powershell, the output will be different.
    Last edited by Mystere; 06 May 2017 at 13:21.
      My ComputerSystem Spec
  10.    06 May 2017 #20

    Quote Originally Posted by Kari View Post
    To read PATH variable in PowerShell:
    $env:
    Yip, that's the way to access those... I don't think the registry route is advisable (for various reasons)

    Anyway.. there is a site called scripting guy or something that dedicate themselves to this stuff...
      My ComputerSystem Spec

 
Page 2 of 3 FirstFirst 123 LastLast


Similar Threads
Thread Forum
Windows 10 Education Pro
Hi sorry if this is the wrong forum, I read an article that states windows 10 pro users in education can upgrade to win10 pro education - Great I thought I registered on the business store applied for the free upgrade - the instructions for the...
Installation and Upgrade
Windows 10 Pro vs Education
So I recently discovered that I can get a copy of Windows 10 Education from my school for free, and I was wondering if that would be a good upgrade. I'm currently running Windows 10 Pro, but based on the few comparison tables I could find Education...
General Support
Windows 10 Education
I work two I.T. jobs. One of them is in a college I.T. department. So, naturally, when I heard about Windows 10 Education, I wanted to find out more. Mainly, the question that popped into my mind was, "Could this be a good system to upgrade to from...
General Support
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 11:47.
Find Us
Twitter Facebook Google+ Ten Forums iOS App Ten Forums Android App



Windows 10 Forums