Can not get powershell script to run automatically. Ultimate goal use Powershell instead of Batch Files for LOGON script in AD Users and Computers RRS feed

  • Question

  • So I don't know why I can't find this or having a hard time understanding running powershell scripts automatically.

    So I created a powershell script called: MapDrive.ps1

    The code inside it:

    >New-PSDrive -Name "L" -Root "\\Server1\SharedD" -Persist -PSProvider "FileSystem"
    >Net Use

    On the computer for testing purposes I ran in powershell the following:

    >Set-ExecutionPolicy RemoteSigned

    I did this for testing purposes to make sure it runs

    I have the script located at the root of C drive to make things easy.

    I right click and Click "Run With PowerShell"

    I get the powershell window and conformation output and now I have a Network Drive mapped to L.

    So Now I run CMD.exe

    On the Commandline I type >powershell.exe ./mapdrive.ps1

    I then enter l: and no go.

    This is what the CMD window has in it:

    ----Start of CMD Window----

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\>powershell.exe ./mapdrive.ps1
    Name           Used (GB)     Free (GB) Provider      Root
    ----           ---------     --------- --------      ----
    L                   7.64         42.36 FileSystem    \\Server1\SharedD
    New connections will be remembered.

    Status       Local     Remote                    Network

    OK           L:        \\Server1\SharedD           Microsoft Windows Network
    The command completed successfully.

    The system cannot find the drive specified.

    ----END of CMD Window----

    What am I doing wrong.


    Second is that ultimately I want to replace the batch file that in used under Active Directory Users and Computers where I right click on a user and goto Profile tab and I remove from the Logon Script window Logon.bat and replace it with Logon.ps1.

    Environment: win server 2012r2 environment with win7 machines moving to win 10 in 5 months

    Is this possible and given the trouble I am having running it locally point me to where I can get info on how I can do this?

    Do I need to keep the batch file and put the execution commands of the powershell I want to use into that?

    Or what would I fill the Logon Script Window with given I have put the new Logon.ps1 in the default Directory the same director that has logon.bat in it.

    Is this only possible using GPO and not the individual users profile? Small company of 8 users so right now did not want to get into GPO. working in steps of moving things there but want to get things working as is then migrate.

    • Edited by MKSTech Monday, March 18, 2019 6:34 PM wrong file name
    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:42 PM Abandoned
    Monday, March 18, 2019 6:32 PM

All replies

  • The drive will not exist in the current CMD session.  You must exit the session and start a new session to get the new environment.

    Here is the correct way to write the command

    New-PSDrive -Name L -Root \\Server1\SharedD -Persist -PSProvider FileSystem -Scope Global


    Monday, March 18, 2019 6:46 PM