Why does Run With Powershell not work?


  1. Posts : 267
    Windows 10
       #1

    Why does Right Click-> Run With Powershell not work?


    Nope, issue still remains. This is my script

    Code:
    # Define the list of network drives to map
        $networkDrives = "\\MyNAS\cracked games", "\\MyNAS\dummy folder", "\\MyNAS\Videos", "\\MyNAS\Images", "\\MyNAS\SysImgs", "\\MyNAS\Szoftver Installacio", "\\MyNAS\EpicGames Games", "\\MyNAS\Ubisoft Connect Games", "\\MyNAS\PlexMediaServer", "\\MyNAS\PLEX Series", "\\MyNAS\PLEX Movies", "\\MyNAS\JLaszlo", "\\MyNAS\STEAM Games", "\\MyNAS\Scripts"
    
        # Here we type in the username and the password
        $Server = Read-Host -Prompt 'NAS nev?'
        $User = Read-Host -Prompt 'Felhasznalonev?'
        $Password = Read-Host -Prompt 'Jelszo?' 
    
    
        # Here we type in the username and the password
        cmdkey /add:$Server /user:network\$User /pass:$Password
    
        # Get the list of used drive letters
        $usedLetters = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name
    
        # Map each network drive using the next available drive letter
        foreach ($drive in $networkDrives) {
            $driveLetter = (65..90 | ForEach-Object { [char]$_ }) | Where-Object { $usedLetters -notcontains $_ } | Select-Object -First 1
            New-PSDrive -Name $driveLetter -Root $drive -Persist -PSProvider FileSystem
            Write-Host "Network drive '$drive' mapped successfully as drive '$($driveLetter):'"
            $usedLetters += $driveLetter
        }
    Can someone tell me why it only runs and does what it's intended to do if I copy-paste the content into a powershell window (standard user, not admin) and click 'Run' (or press Enter)?
    Last edited by Studynxx; 24 Mar 2023 at 13:04.
      My Computer


  2. Posts : 745
    Windows 10/11
       #2

    Studynxx said:
    If I run it as standard user from my home directory (open powershell window -> it says PS C:\Users\MyUsername), it works perfectly. So apparently when I right-click on it and choose Run With Powershell, it doesn't run from inside the user's home directory.
    It sounds like a current directory issue. That is, you probably just need to add a line to the top of the script to make the script's directory the current directory. That line would be:

    set-location $PSScriptRoot

    If you're running PowerShell 2 (i.e. Windows 7 with its installed version of PowerShell), the $PSScriptRoot variable won't exist, so to make the script truly portable, add these TWO lines:

    If (!$PSScriptRoot) {$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path -Parent}
    set-location $PSScriptRoot
      My Computer


  3. Posts : 267
    Windows 10
    Thread Starter
       #3

    LesFerch said:
    It sounds like a current directory issue. That is, you probably just need to add a line to the top of the script to make the script's directory the current directory. That line would be:

    set-location $PSScriptRoot

    If you're running PowerShell 2 (i.e. Windows 7 with its installed version of PowerShell), the $PSScriptRoot variable won't exist, so to make the script truly portable, add these TWO lines:

    If (!$PSScriptRoot) {$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path -Parent}
    set-location $PSScriptRoot
    Nope, issue still remains. This is my script

    Code:
    # Define the list of network drives to map
        $networkDrives = "\\MyNAS\cracked games", "\\MyNAS\dummy folder", "\\MyNAS\Videos", "\\MyNAS\Images", "\\MyNAS\SysImgs", "\\MyNAS\Szoftver Installacio", "\\MyNAS\EpicGames Games", "\\MyNAS\Ubisoft Connect Games", "\\MyNAS\PlexMediaServer", "\\MyNAS\PLEX Series", "\\MyNAS\PLEX Movies", "\\MyNAS\JLaszlo", "\\MyNAS\STEAM Games", "\\MyNAS\Scripts"
    
        # Here we type in the username and the password
        $Server = Read-Host -Prompt 'NAS nev?'
        $User = Read-Host -Prompt 'Felhasznalonev?'
        $Password = Read-Host -Prompt 'Jelszo?' 
    
    
        # Here we type in the username and the password
        cmdkey /add:$Server /user:network\$User /pass:$Password
    
        # Get the list of used drive letters
        $usedLetters = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name
    
        # Map each network drive using the next available drive letter
        foreach ($drive in $networkDrives) {
            $driveLetter = (65..90 | ForEach-Object { [char]$_ }) | Where-Object { $usedLetters -notcontains $_ } | Select-Object -First 1
            New-PSDrive -Name $driveLetter -Root $drive -Persist -PSProvider FileSystem
            Write-Host "Network drive '$drive' mapped successfully as drive '$($driveLetter):'"
            $usedLetters += $driveLetter
        }
    Can someone tell me why it only runs and does what it's intended to do if I copy-paste the content into a powershell window (standard user, not admin) and click 'Run' (or press Enter)?
      My Computer


  4. Posts : 5,325
    Windows 11 Pro 64-bit
       #4

    Let us view the PowerShell error message by executing script from open PowerShell console.
      My Computer


  5. Posts : 267
    Windows 10
    Thread Starter
       #5

    FreeBooter said:
    Let us view the PowerShell error message by executing script from open PowerShell console.
    They're the same EMs that I get when I run it with Run With Powershell. certain network drives arent mapped coz the user doesnt have access to them, that is by design. The problem is that when I run it with Run With Powershell, the network drives that are supposed to be mapped arent mapped
      My Computer


  6. Posts : 745
    Windows 10/11
       #6

    I'm really not sure what's going on with your script, but here's a version that works:
    Code:
    $shares = "\\192.168.1.1\Root", "\\192.168.1.1\Videos"
    
    $Server = Read-Host -Prompt 'Server'
    $User = Read-Host -Prompt 'User'
    $Password = Read-Host -Prompt 'Password'
    
    cmdkey /add:$Server /user:network\$User /pass:$Password
    
    $usedLetters = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name
    
    foreach ($share in $shares) {
      $driveLetter = (68..90 | ForEach-Object { [char]$_ }) | Where-Object { $usedLetters -notcontains $_ } | Select-Object -First 1
      net use $driveLetter":" $share /persistent:yes
      $usedLetters += $driveLetter
    }
    Note that I started the drive letters at D instead of A. Also note that the usual way to map network drives is to start at Z and work backwards, so that the network drive letters don't interfere with removable drives. Net use * does this automatically, so you could simplify the script to:
    Code:
    $shares = "\\192.168.1.1\Root", "\\192.168.1.1\Videos"
    
    $Server = Read-Host -Prompt 'Server'
    $User = Read-Host -Prompt 'User'
    $Password = Read-Host -Prompt 'Password'
    cmdkey /add:$Server /user:network\$User /pass:$Password
    
    foreach ($share in $shares) {
      net use * $share /persistent:yes
    }
      My Computer


 

  Related Discussions
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 05:48.
Find Us




Windows 10 Forums