locked
$PSCommandPath and spaces... RRS feed

  • Question

  • So, this works fine if run within powershell ISE or called from a powershell terminal in such a fashion & .\'file name.ps1' BUT if the script is run by double clicking or right click and opening with and the ps1 file contains spaces in it's name then the $PSCommandPath variable does not pass the path correctly.

    Any ideas?

    #Admin-Loader
    If (-NOT([Security.Principal.WindowsPrincipal]`
      [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
      [Security.Principal.WindowsBuiltInRole] “Administrator”)) 
    {
    $Args = ("'" + $PSCommandPath + "'")
    Start-Process Powershell -Verb runAs -ArgumentList "& $Args"
        Break
    }

    #Script-Start
    • Edited by -Jason- Sunday, October 22, 2017 9:10 PM
    Sunday, October 22, 2017 8:57 PM

All replies

  • Hi,

    Does the $PSCommandPath like the following?
    $PSCommandPath = 'D:\file name.ps1'

    Based on my research, you might not need to add single quotes to the $PSCommandPath variable. Please have a try to pass the variable to -ArgumentList directly to see if the issue remains, the following script for your reference:
    Start-Process -FilePath PowerShell.exe -Verb runas -ArgumentList $PSCommandPath

    In addition, is this script the complete script? It would be helpful for us to identify the problem if you could post the complete script.

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 23, 2017 6:25 AM
  • Hi,

    Just want to confirm the current situations. Have you tried the method provided before?

    If you already tried them or the issue remains after trying them, please don’t hesitate to tell me. I will do more research and try my best to give you helpful suggestions.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, October 26, 2017 5:44 AM
  • Hi Albert, sorry for the late reply.

    1. In regards to $PSCommandPath it passes the script locations as D:\File Name.Ps1 so no encapsulation.

    2. That seems correct, It doesn't actually need the & and it functions fine with just $PSCommandPath but spacing in the file path is still not accepted.

    3. Ill provide the script below, which is run on clean windows 10 installs. The only relevant part is the first 9 lines, it checks to see whether the user context is running within the administrator principle. If the user is in admin mode the script continues if not, then it starts a new powershell running as admin mode and closes the initial launching powershell window.

    I add this "admin-launcher" to any scripts that are run adhoc and require elevation to perform their tasks. I often give these scripts to other people and cannot be sure they will always remember to place the script in a path free of spaces so that is where my issue arises.

    Script

    #Admin-Loader
    If	(-NOT([Security.Principal.WindowsPrincipal]`
    		  [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    		  [Security.Principal.WindowsBuiltInRole] “Administrator”)) 
    	{
    		$Args = ("'" + $PSCommandPath + "'")
    		Start-Process Powershell -Verb runAs -ArgumentList "& $Args"
    		Break
    	}
    
    #Script-Start
    	#Configuring Firewall
    		#Defining Subnets
    		$LocalSubnet = "192.168.0.0/255.255.255.0" 
    	
            #Enabling Windows Firewall
            Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
    
            #Setting Inbound/outbound policy
            Set-NetFirewallProfile -All `
            -AllowInboundRules True `
            -DefaultInboundAction Block `
            -DefaultOutboundAction Allow `
            -LogAllowed False `
            -LogIgnored False `
            -LogBlocked False `
            -NotifyOnListen False
    
            #Clearing Firewall Rules
            Remove-NetFirewallRule -All
    
    		#Defining Rules
    			#File and Printer Sharing
    			New-NetFirewallRule -Name 'FPS-LLMNR-In-UDP' `
    			-DisplayName              'File and Printer Sharing (LLMNR-UDP-In)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow Link Local Multicast Name Resolution. [UDP 5355]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'dnscache' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '5355' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    
    			New-NetFirewallRule -Name 'FPS-ICMP4-ERQ-In' `
    			-DisplayName              'File and Printer Sharing (Echo Request - ICMPv4-In)' `
    			-Description              'Echo Request messages are sent as ping requests to other nodes.' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'Any' `
    			-Protocol                 'ICMPv4' `
    			-LocalPort                'RPC' `
    			-RemotePort               'Any' `
    			-IcmpType                 '8' `
    			-DynamicTarget            'Any'
    
    			New-NetFirewallRule -Name 'FPS-ICMP6-ERQ-In' `
    			-DisplayName              'File and Printer Sharing (Echo Request - ICMPv6-In)' `
    			-Description              'Echo Request messages are sent as ping requests to other nodes.' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'Any' `
    			-Protocol                 'ICMPv6' `
    			-LocalPort                'RPC' `
    			-RemotePort               'Any' `
    			-IcmpType                 '128' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-NB_Datagram-In-UDP' `
    			-DisplayName              'File and Printer Sharing (NB-Datagram-In)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow NetBIOS Datagram transmission and reception. [UDP 138]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'UDP' `
    			-LocalPort                '138' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-NB_Name-In-UDP' `
    			-DisplayName              'File and Printer Sharing (NB-Name-In)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow NetBIOS Name Resolution. [UDP 137]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'UDP' `
    			-LocalPort                '137' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-NB_Session-In-TCP' `
    			-DisplayName              'File and Printer Sharing (NB-Session-In)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow NetBIOS Session Service connections. [TCP 139]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '139' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-SMB-In-TCP' `
    			-DisplayName              'File and Printer Sharing (SMB-In)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow Server Message Block transmission and reception via Named Pipes. [TCP 445]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '445' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-SpoolSvc-In-TCP' `
    			-DisplayName              'File and Printer Sharing (Spooler Service - RPC)' `
    			-Description              'Inbound rule for File and Printer Sharing to allow the Print Spooler Service to communicate via TCP/RPC.' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Spooler' `
    			-Program                  '%SystemRoot%\system32\spoolsv.exe' `
    			-Protocol                 'TCP' `
    			-LocalPort                'RPC' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPS-RPCSS-In-TCP' `
    			-DisplayName              'File and Printer Sharing (Spooler Service - RPC-EPMAP)' `
    			-Description              'Inbound rule for the RPCSS service to allow RPC/TCP traffic for the Spooler Service.' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Rpcss' `
    			-Program                  'Any' `
    			-Protocol                 'TCP' `
    			-LocalPort                'RPCEPMap' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'FPSSMBD-iWARP-In-TCP' `
    			-DisplayName              'File and Printer Sharing over SMBDirect (iWARP-In)' `
    			-Description              'Inbound rule for File and Printer Sharing over SMBDirect to allow iWARP [TCP 5445]' `
    			-Group                    '@FirewallAPI.dll,-28502' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '5445' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			#Netowork Discovery
    			New-NetFirewallRule -Name 'NETDIS-LLMNR-In-UDP-Active' `
    			-DisplayName              'Network Discovery (LLMNR-UDP-In)' `
    			-Description              'Inbound rule for Network Discovery to allow Link Local Multicast Name Resolution. [UDP 5355]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'dnscache' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '5355' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-NB_Datagram-In-UDP-Active' `
    			-DisplayName              'Network Discovery (NB-Datagram-In)' `
    			-Description              'Inbound rule for Network Discovery to allow NetBIOS Datagram transmission and reception. [UDP 138]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'UDP' `
    			-LocalPort                '138' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-NB_Name-In-UDP-Active' `
    			-DisplayName              'Network Discovery (NB-Name-In)' `
    			-Description              'Inbound rule for Network Discovery to allow NetBIOS Name Resolution. [UDP 137]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'UDP' `
    			-LocalPort                '137' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    		
    			New-NetFirewallRule -Name 'NETDIS-FDRESPUB-WSD-In-UDP-Active' `
    			-DisplayName              'Network Discovery (Pub-WSD-In)' `
    			-Description              'Inbound rule for Network Discovery to discover devices via Function Discovery. [UDP 3702]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'fdrespub' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '3702' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-SSDPSrv-In-UDP-Activee' `
    			-DisplayName              'Network Discovery (SSDP-In)' `
    			-Description              'Inbound rule for Network Discovery to allow use of the Simple Service Discovery Protocol. [UDP 1900]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Ssdpsrv' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '1900' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-UPnPHost-In-TCP-Active' `
    			-DisplayName              'Network Discovery (UPnP-In)' `
    			-Description              'Inbound rule for Network Discovery to allow use of Universal Plug and Play. [TCP 2869]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '2869' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-WSDEVNT-In-TCP-Active' `
    			-DisplayName              'Network Discovery (WSD Events-In)' `
    			-Description              'Inbound rule for Network Discovery to allow WSDAPI Events via Function Discovery. [TCP 5357]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '5357' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-WSDEVNTS-In-TCP-Active' `
    			-DisplayName              'Network Discovery (WSD EventsSecure-In)' `
    			-Description              'Inbound rule for Network Discovery to allow Secure WSDAPI Events via Function Discovery. [TCP 5358]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  'System' `
    			-Protocol                 'TCP' `
    			-LocalPort                '5358' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-FDPHOST-In-UDP-Active' `
    			-DisplayName              'Network Discovery (WSD-In)' `
    			-Description              'Inbound rule for Network Discovery to discover devices via Function Discovery. [UDP 3702]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'fdphost' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '3702' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    			New-NetFirewallRule -Name 'NETDIS-DAS-In-UDP-Activee' `
    			-DisplayName              'Network Discovery (WSD-In)' `
    			-Description              'Inbound rule for Network Discovery to discover devices via Device Association Framework. [UDP 3702]' `
    			-Group                    '@FirewallAPI.dll,-32752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'Any' `
    			-Program                  '%SystemRoot%\system32\dashost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '3702' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    
    			#Remote Desktop
    			New-NetFirewallRule -Name 'RemoteDesktop-UserMode-In-TCP' `
    			-DisplayName              'Remote Desktop - User Mode (TCP-In)' `
    			-Description              'Inbound rule for the Remote Desktop service to allow RDP traffic. [TCP 3389]' `
    			-Group                    '@FirewallAPI.dll,-28752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'termservice' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'TCP' `
    			-LocalPort                '3389' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    			
    			New-NetFirewallRule -Name 'RemoteDesktop-UserMode-In-UDP' `
    			-DisplayName              'Remote Desktop - User Mode (UDP-In)' `
    			-Description              'Inbound rule for the Remote Desktop service to allow RDP traffic. [UDP 3389]' `
    			-Group                    '@FirewallAPI.dll,-28752' `
    			-Enabled                  'True' `
    			-Profile                  'Private' `
    			-Direction                'Inbound' `
    			-Action                   'Allow' `
    			-LocalAddress             $LocalSubnet `
    			-RemoteAddress            $LocalSubnet `
    			-Service                  'termservice' `
    			-Program                  '%SystemRoot%\system32\svchost.exe' `
    			-Protocol                 'UDP' `
    			-LocalPort                '3389' `
    			-RemotePort               'Any' `
    			-IcmpType                 'Any' `
    			-DynamicTarget            'Any'
    		
    		#Nulling Variables
            $LocalSubnet = $null
    		
    	#Poweroptions
    	powercfg -h off
    	
    	#Setting registry keys
            #Initialize an array
            $RegistryEntries = @()
    
                #Create a PSObject with the associated properties hashtable
                    #Disabling Xbox Capture
                    $RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR';
                                                                 Name         = 'AppCaptureEnabled';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Disabling Xbox Capture
    				$RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR';
                                                                 Name         = 'AudioCaptureEnabled';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Disabling Xbox Capture
    				$RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\GameDVR';
                                                                 Name         = 'HistoricalCaptureEnabled';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Disabling Xbox Game Mode
    				$RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\GameBar';
                                                                 Name         = 'AllowAutoGameMode';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    
                    #Disabling Xbox GameDVR overlay
                    $RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_CURRENT_USER\SYSTEM\GameConfigStore';
                                                                 Name         = 'GameDVR_Enabled';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    
                    #Disabling Cortana
                    $RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Windows Search';
                                                                 Name         = 'AllowCortana';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    
                    #Disabling Cortana Lock Screen
                    $RegTable = new-object PSObject -Property @{ Key          = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Windows Search';
                                                                 Name         = 'AllowCortanaAboveLock';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    
                    #Disabling Start Menu suggested content
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\CloudContent';
                                                                 Name         = 'DisableWindowsConsumerFeatures';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '1'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer LaunchTo
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
                                                                 Name         = 'LaunchTo';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '1'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer ShowRecent
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer';
                                                                 Name         = 'ShowRecent';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    
                    #Setting Explorer ShowFrequent
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer';
                                                                 Name         = 'ShowFrequent';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer HideFileExt
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
                                                                 Name         = 'HideFileExt';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer Hidden
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
                                                                 Name         = 'Hidden';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '1'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer HideDrivesWithNoMedia
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced';
                                                                 Name         = 'HideDrivesWithNoMedia';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '0'}
                    $RegistryEntries += $regtable
    				
    				#Setting Explorer Autoplay
                    $RegTable = new-object PSObject -Property @{ Key          = 'REGISTRY::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers';
                                                                 Name         = 'DisableAutoplay';
                                                                 PropertyType = 'Dword';
                                                                 Value        = '1'}
                    $RegistryEntries += $regtable
    
            #Invoking registry changes
            ForEach ($line in $RegistryEntries)
            {
                IF ((Test-Path -Path $line.key)-ne "True")
                {
    				New-Item -Path $line.key | out-Null
                }
                New-ItemProperty -Force -Path $line.key -Name $line.name -Value $line.value -PropertyType $line.propertytype
            }
    
            #Nulling Variables
            $line = $null
            $RegistryEntries = $null
            $RegTable = $null
    
    
    	#Configuring Services
        $StopServiceArray = @('HomeGroupListener';
                              'HomeGroupProvider')
                         
        ForEach ($line in $StopServiceArray)
            {
                Stop-Service -Name $line
                Set-Service -Name $line -Status Stopped -StartupType Disabled
            }
    
            #Nulling Variables
            $line = $null
            $StopServiceArray = $null
    
    	#uninstalling store apps
        $WindowsApps = @('Microsoft.3DBuilder',
    					 'Microsoft.BingWeather',
    					 'Microsoft.DesktopAppInstaller',
    					 'Microsoft.GetHelp',
    					 'Microsoft.Getstarted',
    					 'Microsoft.Messaging',
    					 'Microsoft.Microsoft3DViewer',
    					 'Microsoft.MicrosoftOfficeHub',
    					 'Microsoft.MicrosoftSolitaireCollection',
    					 'Microsoft.Office.OneNote',
    					 'Microsoft.OneConnect',
    					 'Microsoft.People',
    					 'Microsoft.Print3D',
    					 'Microsoft.SkypeApp',
    					 'Microsoft.WindowsAlarms',
    					 'Microsoft.WindowsCamera',
    					 'microsoft.windowscommunicationsapps',
    					 'Microsoft.WindowsFeedbackHub',
    					 'Microsoft.WindowsMaps',
    					 'Microsoft.WindowsSoundRecorder',
    					 'Microsoft.Xbox.TCUI',
    					 'Microsoft.XboxApp',
    					 'Microsoft.XboxGameOverlay',
    					 'Microsoft.XboxIdentityProvider',
    					 'Microsoft.XboxSpeechToTextOverlay',
    					 'Microsoft.ZuneMusic',
    					 'Microsoft.ZuneVideo',
    					 'king.com.CandyCrushSodaSaga',
    					 'Facebook.Facebook',
    					 'Microsoft.BingNews',
    					 'Microsoft.MinecraftUWP',
    					 'A278AB0D.MarchofEmpires',
    					 '9E2F88E3.Twitter')
    
    	foreach ($App in $WindowsApps)
    		{
    			Get-AppxPackage -Name $App | Remove-AppxPackage -ErrorAction SilentlyContinue | Out-Null
    			Get-AppXProvisionedPackage -Online | Where-Object DisplayName -eq $App | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue | Out-Null
    		}
    	
    		#Nulling Variables
    		$WindowsApps = $null
    		$App = $null


    • Edited by -Jason- Saturday, October 28, 2017 3:52 AM
    Saturday, October 28, 2017 3:33 AM
  • This is the error when running the script in a path or filename that contains spacing.

    Image

    The script seems to know the required location as the 5th line makes a reference, but after the new powershell instance is opened the path is plain.

    Saturday, October 28, 2017 3:43 AM
  • When a script is launched from a right co text menu in File Explorer the filename is passed to PowerShell on the command line.  This will not behave correctly when trying to re-launch as an elevated admin.  This has nothing to do with spaces in the file name but it has to do with your usage.

    Start-Process -Verb RunAs -FilePath Powershell  -ArgumentList "-File $args[0]"


    \_(ツ)_/

    Saturday, October 28, 2017 4:55 AM
  • Hi jrv, thanks for the correction, however i'm not sure if i'm getting the intended results?

    when using "-file $args[0]" with the file path being c:\users\jason\desktop\file name.ps1 it seems to only pass on C which is position [0] in $args?

    Saturday, October 28, 2017 5:35 AM
  • Again this is how arguments are parsed when using the right context launch.  The answer is to not use spaces in executable names ever.

    You can try this:

    start-process -FilePath powershell -Verb RunAs -ArgumentList "-NoExit -File $($MyInvocation.InvocationName)"

    or this:

    start-process -FilePath powershell -Verb RunAs -ArgumentList @('-NoExit','-File',"$($MyInvocation.InvocationName)")


    \_(ツ)_/


    Saturday, October 28, 2017 6:20 AM