New
#111
That's even better @Matthew Wai, thank you.![]()
Hello @Matthew Wai,
Just a quick question, what is the difference between . . .
Code:(Net Session >NUL 2>&1)||(PowerShell Start -verb RunAs '%~0' & Exit /b)Thanks in advance.Code:(Net Session >NUL 2>&1)||(PowerShell Start """%~0""" -verb RunAs & Exit /b)
Try to figure out this thread: PowerShell start 'D:\Matthew's scripts\Testing.cmd'
If you can't figure it out, I will try to explain it.
Hello @Matthew Wai,
My question is why the first one was used for the BAT files is different to the second one used for the REG files.
Is it to escape any special characters [spaces etc] because of literal interpretations etc?
The difference between the two is that the first one shows -verb RunAs '%~0' and the second one shows """%~0""" -verb RunAs. Could I use """%~0""" -verb RunAs in BOTH?
Thanks in advance.
Because I did not know the second one when I used the first one. Now I know the second one is better than the first one.
Yes.
It does not work if the file path contains apostrophes.
It works even if the file path contains apostrophes.
Hello @Matthew Wai,
As you know the final part of this is to run ALL .ps1 files from a folder.
I have come up with this but it continuously opens blank Windows . . .
Thanks in advance.Code:@echo off (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B) Set "Folder=C:\Users\System-Admin\System-Admin_Implement" For %%i In ("%Folder%\*.ps1") Do PowerShell -Command (echo Processing "%%i"&CMD /C "%%i" & echo.) echo Press a key to restart "explorer.exe". & pause > nul taskkill /f /im explorer.exe & start explorer.exe
This part is much more difficult! An expert can certainly do it much better than I do. I can only come up with the following awkward method:
Code:@echo off (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B) If exist "%temp%\✱.vbs" (del "%temp%\✱.vbs") Echo Set X = CreateObject("Wscript.shell")>>"%temp%\✱.vbs" Set "Folder=C:\Users\System-Admin\System-Admin_Implement" For %%i In ("%Folder%\*.ps1") Do (Echo File found: "%%i" Echo X.run ^"Powershell -executionpolicy bypass -file ^" ^& chr^(34^) ^& "%%i" ^& chr^(34^), 0, true >> "%temp%\✱.vbs") Echo Processing the files. & "%temp%\✱.vbs" Echo Operations completed. & Del "%temp%\✱.vbs" Echo Press a key to restart "explorer.exe". & pause > nul taskkill /f /im explorer.exe & start explorer.exe
Hello @Matthew Wai,
I came up with this that part works!
The trouble is it lists everything within the file and does NOT list just the filename etc.Code:@echo off Set "Folder=C:\Users\System-Admin\System-Admin_Implement" rem PowerShell -Command "& {Set-ExecutionPolicy ByPass}" -NoExit rem For %%i in (*.ps1) Do PowerShell -Command "& {.\%%i}" -NoExit For %%i In (%Folder%\*.ps1) Do PowerShell -Command "& {%%i}" echo Press a key to restart "explorer.exe". & Pause > NUL TaskKill /f /im explorer.exe & Start explorer.exe
Thanks in advance.
It does not work at my end. It produced the following error:
Code:C:\Users\System-Admin\System-Admin_Implement\PS1.ps1 : File C:\Users\System-Admin\System-Admin_Implement\PS1.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:4 + & {C:\Users\System-Admin\System-Admin_Implement\PS1.ps1}