Elevate rights in current directory with PowerShell - windows

For example I'm in C:\Users\User\Desktop\Tools and I'm trying to stay here as admin.
I tried this way, gluing together different commands:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -noexit" & -windowstyle hidden -Command Start-Process powershell -ArgumentList '-NoExit', '-Command cd %V' -Verb runAs""
The PATH changes to C:\WINDOWS\system32 why? How to elevate rights in current directory via simple command?

Use $PWD from the calling process to change the location on startup:
Start-Process powershell -ArgumentList '-NoExit', "-Command cd '$pwd'; & .\actual\script\you\want\to\run.ps1" -Verb runAs

Related

powershell error while trying to start process

Running PS as admin, trying to run an exe from server (or even if i copy this file to my computer and trying to run it locally)
$apps = "\srv\blabla"
Start-Process $apps\mbsetup.exe -Credential $Credentials
Error:
without -credential this would work, i would assume something bad with the user definition but the creds are that of the domain admin.
any idea?
It looks like your app requieres administrative privileges. Add -Verb RunAs to your Start-Process cmdlet to elevate the execution.
But -Verb RunAs will not work in combination with -Credential. This is a workaround:
Start-Process powershell -Credential $Credentials -ArgumentList "-Command &{Start-Process yourApp.exe -Verb RunAs}"

How to automatically add a computer to a domain from a Powershell script using a non-administrator user

I have a number of computers that will need to be added to our domain. The issue is the user that these computers log in with are not administrator users. We do have administrator users on the machines though.
I am trying to have a Powershell script run from a batch script, but nothing I do is giving me the correct results. It seems that even if the Powershell script runs as an administrator, because the batch script is not run as an administrator (it is run automatically on startup), I get a credential error. I've tried running a self-elevating PS script, but to no avail.
Here is one of the batch file lines I've tried:
#echo off
SET thisdir=%~dp0
SET pspath=%thisdir%domain_add.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process Powershell -argumentlist '-NoProfile -ExecutionPolicy Bypass -File ""%pspath%""' -verb runas}";
I've even tried using the batch file to start a powershell script, which then calls the add domain PS script:
$user = '.\alohaadmin'
$passfile = 'alohapass.txt'
$password = Get-Content $passfile | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
Start-Process 'powershell.exe' -Credential $cred -ArgumentList '-noexit','-file "domain_add.ps1"'
The issue seems to be with the initial batch file not being run as an admin.

Executing file from Admin CMD via CLI

I am trying to execute a file via the CMD with Administrative privileges.
How can I open a cmd via command line with Administrative privileges.
I have to execute a script within a script.
powershell -ExecutionPolicy ByPass -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -File C:\scripts\install_ims.ps1' -verb RunAs}"
I have tried this in a bat file, but it doesn't work when executed within the script.
This should work fine for your purposes.
powershell -Command "Start-Process <filename> -Verb RunAs"
This is copy-pasted from some Batch files that I finally added to GitHub in the last few days if you have questions, that's probably the best place to go. https://github.com/Benny121221/Batch-BS

How to start PowerShell script from BAT file with proper Working Directory?

I'm trying to create bat script that can start PowerShell script named the same as bat file in proper working directotry.
This is what I got:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -WorkingDirectory '%~dp0' -Verb RunAs}"
PAUSE
Passing working directory this way does not work.
How to make script that will pass proper working directroy and also command line arguments?
The -WorkingDirectory parameter doesn't work when using -Verb RunAs. Instead, you have to set the working directory by calling cd within a -Command string.
This is what I use: (cmd/batch-file command)
powershell -command " Start-Process PowerShell -Verb RunAs \""-Command `\""cd '%cd%'; & 'PathToPS1File';`\""\"" "
If you want to make a "Run script as admin" right-click command in Windows Explorer, create a new registry key at HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Run with PowerShell (Admin)\Command, and set its value to the command above -- except replacing %cd% with %W, and PathToPS1File with %1 (if you want it to execute the right-clicked file).
Result: (Windows Explorer context-menu shell command)
powershell -command " Start-Process PowerShell -Verb RunAs \""-Command `\""cd '%W'; & '%1';`\""\"" "
EDIT: There's an alternative way to have the script be run as admin from Explorer, by using the "runas" sub-key: https://winaero.com/blog/run-as-administrator-context-menu-for-power-shell-ps1-files
If you want to run your script as admin from an existing powershell, remove the outer powershell call, replace %W with $pwd, replace %1 with the ps1 file-path, and replace each \"" with just ".
Note: The \""'s are just escaped quotes, for when calling from the Windows shell/command-line (it's quote-handling is terrible). In this particular case, just \" should also work, but I use the more robust \"" for easier extension.
See here for more info: https://stackoverflow.com/a/31413730/2441655
Result: (PowerShell command)
Start-Process PowerShell -Verb RunAs "-Command `"cd '$pwd'; & 'PathToPS1File';`""
Important note: The commands above are assuming that your computer has already been configured to allow script execution. If that's not the case, you may need to add -ExecutionPolicy Bypass to your powershell flags. (you may also want -NoProfile to avoid running profile scripts)
A workaround is to let the PowerShell script change the directory to it's own origin with:
Set-Location (Split-Path $MyInvocation.MyCommand.Path)
as the first command.
As per mklement0s hint: In PSv3+ use the simpler:
Set-Location -LiteralPath $PSScriptRoot
Or use this directory to open adjacent files.
$MyDir = Split-Path $MyInvocation.MyCommand.Path
$Content = Get-Content (Join-Path $MyDir OtherFile.txt)

Unable to execute multi-command in power shell

I'm working on power shell script using windows 2012 server, that do simple two functions
open powershell as an administrator
change the directory to c:\user\scrpt.bat
the code is:
powershell -Command "& {powershell Start-Process PowerShell -Verb RunAs; Set-Location C:\}
the problem is not when execute the first part, it's in the other part which is:
Set-Location C:\}
My question is there any way after running powershell as administrator execute the next command ?
I already tried to use semicolon ";" but no luck
If you want to change directory for the process you're spawning - use -WorkingDirectory option:
powershell -Command "& { Start-Process PowerShell -Verb RunAs -WorkingDirectory 'D:' }"

Resources