New
#11
Good
It would not run as admin unless you had made it run as admin [and had approved it at the admin dialog]
Be careful what your refer to as running "successfully". If the commands ran "successfully" then you would have finished the job.
Regsvr32 does need admin privileges - see Regsvr32 - Register a DLL - SS64
How are you getting a run as admin option? It should not be there for ps1 files. If it is there, because you added this to your context manu manually, then ignore it because it will not do what you think it does.
This test was completely meaningless. It was a waste of your time.
Good.
When you double-click what - the shortcut?
Did the admin prompt dialog appear?
Stick an extra line at the end of the ps1 file as a test - just write the word pause so that the window should remain open when the commands finish.
You shouldn't be getting any errors at this stage because you tested the script in a powershell window earlier on - unless I misunderstood you.
Denis
Personally, if I was going to sit down and study something it would be powershell not individual commands in batch files.
Here's an introduction to batch files - The Command Line in Windows Batch file basics
And here are some guides by the excellent RobvdWoude
Batch files - RobvdW
Batch File Scripting Techniques - RobvdW
Batch Files Examples - RobvdW
and some reference guides on individual commands
<command> /? produces an up-to-date Help file for most commands, this is the only known source for up-to-date, Windows10-specific Help.
Command-Line Reference - MSDocs {note that the guidance might reflect variants of the command such as older versions or non-WIndows10 versions}
Windows command index - SS64 {note that the guidance might reflect variants of the command such as older versions or non-WIndows10 versions}
Commands forum - SS64
Just to emphasise the point, the Windows 10 netsh command is a significant step forward from its earlier versions but there is no online guide for this version. All online netsh guides are out-of-date or refer to non-Windows10 variants of the command.
And powershell guides -
PowerShell Scripting - The Basics - TenForumsTutorials
PowerShell Scripting - Run a Script from Shortcut - TenForumsTutorials
PowerShell commands index - SS64
PowerShell forum - SS64
PowerShell Tutorials - Windows10Forums [2017] - I am normally wary of this website for reasons that I cannot remember [lots of content copied from this forum possibly?] but their PS tutorial seems OK.
Mastering PowerShell with Dr. Tobias Weltner [2009] - I've just added this one. Its original website was no longer available so I had to track it down.
PowerShell Commands - ScriptRunner
And remember - if it doesn't feel like you're banging your head against the wall, it isn't proper programming.
All the best,
Denis
Last edited by Try3; 10 Jan 2021 at 06:37.
When you read other people's batch files, you will see variables used in ways that seem peculiar & unintelligible.
- For Windows commands and batch files, variables are enclosed in % and sometimes %% [and you might sometimes even see %%%]
- You might see, and this is just for example, Set MyVar=%MyVar:*cd=% This does actually mean something and can be very useful. It is one of the examples explained below.
- Once you get the hang of manipulating variables in batch files you will be able to write very powerful procedures.
- Once you get the hang of manipulating variables in batch files you will be able to adapt very easily to manipulating variables in better structured scripting such as powershell.
This is a guide to manipulating batch files variables that I put together [based on a source whose identity I have rudely mislaid - it might have been RobvdW or an early version of Windows Batch Scripting - Wikibooks]
- Downloadable version ManipulatingVariables-UsefulExamples.bat.txt
- The file ManipulatingVariables-UsefulExamples.bat.txt is deliberately laid it out so it can be saved for reading as a text file [.txt] or for running as an innocuous batch file [.bat] - it is just a demonstrator, it displays variables onscreen but does not alter anything else at all. So it is safe to run. Just change its file extension for what you want on any given occasion [txt/bat] - no other changes are necessary.
- It pauses after every example so you can examine each one in sequence.
Code:REM This file can be read as a text file by keeping its file extension as .txt or run as a batch file by changing its file extension to .bat {the contents do not need changing} REM The batch file is an innocuous demonstrator - it displays variables onscreen but does not alter anything outside of itself REM I end up referring to this file for almost every batch script I write REM I think my original source for learning to manipulate variables was 'Windows Batch Scripting - Wikibooks' Windows Batch Scripting - Wikibooks, open books for an open world REM - and it was that source that inspired me to compile this ready-reference list of examples REM The MyVar variable is used as the input in all the examples that follow REM and the output variable for each example is NewVar REM except for the 'Test for' examples - these just refer to MyVar REM ::::: The variable used as the example ::::: Set MyVar=abcdefghijklm Echo %MyVar% REM MyVar echo is abcdefghijklm Pause REM ::::: Extracting characters ::::: REM Extract the first character REM Note - The first character is at location 0 [not location 1] Set NewVar=%MyVar:~0,1% Echo %NewVar% REM NewVar echo is a Pause REM Extract the second character Set NewVar=%MyVar:~1,1% Echo %NewVar% REM NewVar echo is b Pause REM Extract the second and third characters Set NewVar=%MyVar:~1,2% Echo %NewVar% REM NewVar echo is bc Pause REM Extract the last character Set NewVar=%MyVar:~-1% Echo %NewVar% REM NewVar echo is m Pause REM Extract the last two characters Set NewVar=%MyVar:~-2% Echo %NewVar% REM NewVar echo is lm Pause REM Extract the second though to second-to-last characters Set NewVar=%MyVar:~1,-1% Echo %NewVar% REM NewVar echo is bcdefghijkl Pause REM Extract the second though to third-to-last characters Set NewVar=%MyVar:~1,-2% Echo %NewVar% REM NewVar echo is bcdefghijk Pause REM ::::: Removing characters ::::: REM Remove the first character Set NewVar=%MyVar:~1% Echo %NewVar% REM NewVar echo is bcdefghijklm Pause REM Remove the first and second characters Set NewVar=%MyVar:~2% Echo %NewVar% REM NewVar echo is cdefghijklm Pause REM Remove the last character Set NewVar=%MyVar:~0,-1% Echo %NewVar% REM NewVar echo is abcdefghijkl Pause REM Remove the last two characters Set NewVar=%MyVar:~0,-2% Echo %NewVar% REM NewVar echo is abcdefghijk Pause REM Remove all occurences of a particular character REM For this example, I have chosen to remove c Set NewVar=%MyVar:c=% Echo %NewVar% REM NewVar echo is abdefghijklm Pause REM Remove all occurences of a particular character sequence REM For this example, I have chosen to remove bc Set NewVar=%MyVar:bc=% Echo %NewVar% REM NewVar echo is adefghijklm Pause REM Remove all characters up to and including the first occurence of a particular character REM This can only be used from the start of a string not in mid position or from the end REM For this example, I have chosen to remove everything up to and including c Set NewVar=%MyVar:*c=% Echo %NewVar% REM NewVar echo is defghijklm Pause REM Remove all characters up to and including the first occurence of a particular character sequence REM This can only be used from the start of a string not in mid position or from the end REM For this example, I have chosen to remove everything up to and including cd Set NewVar=%MyVar:*cd=% Echo %NewVar% REM NewVar echo is efghijklm Pause REM ::::: Testing for particular characters ::::: REM Note that == means compare text values of variables REM Test for the presence of a particular character in the string REM Note - Only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above] REM Notice that this example compares an extract from MyVar with the whole of MyVar REM For this example, I have chosen to test for b If Not "%MyVar:b=%"=="%MyVar%" Echo %MyVar% contains b REM MyVar echo is abcdefghijklm contains b REM What this does is compare an extract of (Myvar with all letter b characters removed) with (the whole of MyVar) REM - if the extract and the whole variable match then there were no letter b characters in the variable in the first place REM - if the extract and the whole variable do not match then there was at least one letter b character removed from the variable Pause REM Test for the presence of a particular character sequence in the string REM only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above] REM The characters are chosen using the Extract character[s] example syntax above REM Notice that this example compares an extract from MyVar with the whole of MyVar REM For this example, I have chosen to test for bcd If Not "%MyVar:bcd=%"=="%MyVar%" Echo %MyVar% contains bcd REM MyVar echo is abcdefghijklm contains bcd REM What this does is compare an extract of (Myvar with all letter bcd characters removed) with (the whole of MyVar) REM - if the extract and the whole variable match then there were no letter sequences bcd in the variable in the first place REM - if the extract and the whole variable do not match then there was at least one letter sequence bcd removed from the variable Pause REM Test for a particular character sequence at the start or another specific location within the variable REM The characters are chosen using the Extract character[s] example syntax above REM This particular example tests the two characters at the start of the variable REM Notice that this example compares (an extract from MyVar) with (the whole of MyVar) REM For this example, I have chosen to test for ab at the start of the variable If "%MyVar:~0,2%"=="ab" Echo %MyVar% starts with ab REM MyVar echo is abcdefghijklm starts with ab Pause REM ::::: Replacing characters ::::: REM Replace all occurences of a particular character - first of two examples of this REM For this example, I have chosen to replace c with X Set NewVar=%MyVar:c=X% Echo %NewVar% REM NewVar echo is abXdefghijklm Pause REM Replace all occurences of a particular character - second of two examples of this REM For this example, I have chosen to replace c with XYZ Set NewVar=%MyVar:c=XYZ% Echo %NewVar% REM NewVar echo is abXYZdefghijklm Pause REM Replace all occurences of a particular character sequence - first of two examples of this REM For this example, I have chosen to replace bc with XY Set NewVar=%MyVar:bc=XY% Echo %NewVar% REM NewVar echo is aXYdefghijklm Pause REM Replace all occurences of a particular character sequence - second of two examples of this REM For this example, I have chosen to replace bc with X Set NewVar=%MyVar:bc=X% Echo %NewVar% REM NewVar echo is aXdefghijklm Pause
Denis
Last edited by Try3; 03 Apr 2020 at 16:22.
phrab,
I have been changing some text within the code of my last post [onscreen & its downloadable version] in an attempt to improve the explanations. I have now stopped fiddling so my last post now contains the final versions.
It's the first time I have edited that file since 2013 so I suppose it was due.
Denis
I have just added another PS book to the list
Mastering PowerShell with Dr. Tobias Weltner [2009] - Its original website was no longer available so I had to track it down.
Denis
phrab,
I have added some guidance about using standard passable variables with batch files at
my ditty and demo for Standard passable variables [post #47] - TenForums
Denis
Paul,
How very nice of you to say so.
I assume you found this thread after you looked at Batch file and PowerShell guides [post #16] - TenForums
All the best,
Denis