How can I find a Powershell's running directory in a GPO? - windows

I have a powershell script that runs in a GPO. It needs to open a text file, also inside that GPO. I can't get the path information for the text file because the script doesn't execute in the same directory the script is.
I need to open the text file somehow. How do I do it?

I can't fully understand what you're trying to do, but if I've guessed right then you want the path of the script while its been executed?
If that's the case then use this variable:
$MyInvocation.MyCommand.Path

Related

Running python script on CMD which uses a folder to output results

I was using Pycharm as my editor to run the scripts since i need to add a task scheduler i had to test the code on the command prompt. Firstly this is the structure of my project.
When i try to run the following line
C:\Users\My_name\AppData\Local\Programs\Python\Python36-32\python.exe "C:\Users\My_name\PycharmProjects\FYP_CB006302\generateSummary.py"
I get this error,
From the knowledge i have i think it is because it doesn't recognize the path.
But when i change the directory to my project folder then give the path to python.exe and type generateSummary.py it works which was done as shown here.
However i highly doubt that this method can be used to task a schedule in Windows. Therefore, any ideas that would to run as shown in the beginning will be helpful.
The problem here is that when you use that line to run a particular script the folder which is causing the error is out of scope.
C:\Users\My_name\AppData\Local\Programs\Python\Python36-32\python.exe "C:\Users\My_name\PycharmProjects\FYP_CB006302\generateSummary.py"
In this case pickle_saves folder is out of scope. You can avoid this by giving a absolute path to that file in line 173. Where the absolute path is something like C:\user\documents\projects\pickle_saves\all_words

prevent Windows .bat file from quiting

I have a very simple Windows .BAT file:
set PATH=c:\xxx;%PATH%
call foo.pl
set VAR=true
I thought "call" will start a new batch process, without affecting the current one. However, the batch file exited immediately after the foo.pl finished executing. The set VAR=true has never been called.
Is there a way to fix it?
foo.pl is not a batch file, it is a Perl script.
So you need to use
path c:\xxx;%PATH%
"Path to\Folder With\perl.exe" "foo.pl"
rem Additional batch code executed after Perl script execution finished.
In other words you have to run the console application perl.exe best with full path, or with just perl.exe if program files folder of Perl is not the same on all computers on which this batch file is used and hopefully PATH contains also the directory containing perl.exe.
If you specify on a command line or in a batch file just foo.pl, Windows looks in Windows registry which application is associated with .pl for action Open. If there is such a file association, Windows runs this application in a separate process like when using command start.
So using call foo.pl is like using start foo.pl.
PATH is not only an environment variable, but also an interal command written for changing the value of environment variable PATH at any time within a batch file. This is the reason why I removed set from first line. It is better to use internal command path for modifying environment variable PATH.

How do I run a global command with same name as a file in the working directory on windows?

I want to run a command that is installed and available globally on my cmd.exe commandline.
This usually works fine, except when I run it in a directory that has a file with the same name as the command.
So any time I use this command in this particular directory my windows is trying to open this file in whatever application is registered for this extension.
It is very annoying, but there must be a way around this right?
I tried it with a bunch of names, like ping.txt and they all open the files intead of running the command.
That's not normal behaviour in the default configuration; sounds like the PATHEXT environment variable has been modified.
You could either change it back to the default,
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
or explicitly type ping.exe (for example) instead of just ping.
If the file name is EXACTLY the same (including the same extension, i.e. ".exe"), then I believe the only way around this is to specify the full path to the file you WANT to execute. For example, if the program you want to execute is explorer.exe, but you have a file named explorer.exe in your current directory, you have to specify \Windows\explorer.exe to run Windows explorer.

How to create and execute a file full of commands on Windows command prompt?

Example:
In Linux we can put the desired commands in a file and give it executable permissions. This helps us to actually run the file on the terminal and thus all the commands inside the file get automatically executed.
How to achieve this on Windows XP?
Same thing, but it's called a batch file, extension is .bat. You can also double-click to run these. This site is a great resource.

Windows PATH variable

My program needs .bat file to run because the .bat file is changing some language settings, so .bat file looks like:
set lang=en
start ap.exe
It is working great but when I made installer for my app and pointed .bat file as main file, it creates a shortcut on the desktop to that .bat file. So far everything is great but when I launch that shortcut it cannot open app.exe because it can't find it on desktop.
So my question is: How can I get path to folder of .bat file so I could set proper start command? Something like:
set lang=en
S=getpath();
start S/app.exe
It is just pseudocode but I think you get point.
You can write %~dp0 to get the directory containing the batch file.
Therefore, you can write
"%~dp0app.exe"

Resources