Powershell get case sensitive path as displayed in windows - windows

I'm looking for a way to get the my local path that includes any camel cases that are used in the names. The primary reason is so I can use the same path to make a call in windows wsl. For example, in windows I can call a file as
c:\FoO\bar.txt
c:\Foo\Bar.txt
and windows will display it as c:\foo\bar.txt. When I try to enter the WSL with bash I need to know the actual path since Linux is case sensitive.
I have tried using
$PSScriptRoot
Split-path C:\FoO\Bar.txt
(get-Item c:\Foo\Bar.txt).FullName
but they will only provide the path that to call the script.
How do I get my path as it's displayed in the windows os?
I can't just call the full path of the file I need since I can't guarantee the root directory it starting from. I also don't want to burn up cycles doing a find.

What you want is to look at the Target property you get back from Get-Item. Fullname will come back however you typed it initially, but Target is actually a code property that seems to get the raw path of the object.
(get-Item c:\Foo\Bar.txt).Target

Find the directory with Windows File Explorer and it shows full path name.
Find the directory in WSL or PowerShell and "pwd" or "echo $PWD" gives full path name. Add directory to PATH in $HOME/.profile and you don't need full path name.

I have some automated ways to do it.
See http://ContextKnowledge.blog
"myenv package for Windows 10 + Cygwin + WSL/Ubuntu"
The package includes a few short shell scripts
which find the information and record it in environment variables.
If these shell scripts don't work for you, contact me with more details
and I'll figure out something that will.

Related

What is the difference between the PATH listed by 'env' in git-bash and the PATH in Windows 10's control panel?

My goal is to write a shell script that will ensure that a bunch of Windows 10 computers with Python freshly installed on them can run the 'python' command from a git-bash command line in Windows Terminal by having the script check the PATH environment variable and modify it if necessary.
More specifically, I want my script to check if the following three paths are part of each computer's PATH, and if they aren't already a part of it, edit the PATH so that it permanently contains them.
C:\Program Files\Python311
C:\Program Files\Python311\Scripts
C:\Users\ <localUser>\AppData\Roaming\Python\Python311\Scripts
What threw me off is that I noticed that the PATH variable that I get from the 'env' command on the git-bash command line is different from the PATH variable that I can see in Windows 10's control panel. I tried adding a junk path to PATH from the command line by typing
export PATH=/c/someNonexistentPath in git bash but it didn't change the PATH that I could see in the Windows 10 control panel. Moreover, I couldn't even see if it changed the PATH I get from running env on the command line because for some reason after you run any export command git-bash gets amnesia and refuses to recognize the env command until you start a new session of git-bash.
Shouldn't there only be one PATH on a computer? If the two seemingly different PATHs that I have mentioned are not supposed to be the same, what is the difference between them, and how can I accomplish my goal of writing the script so that it does what I need it to do?
Please let me know if I need to include any more system-specific info. Thanks in advance for your help.

Run .exe anywhere in cmd without PATH variable

This works (Notepad++):
C:\Anywhere> start notepad++ hello.txt
And this works (SoX for removing silence in sounds):
C:\Anywhere> sox in.wav out1.wav silence 1 0.1 1%
Yet, my PATH variable includes neither (would send on request).
How can I do this with my program?
To run from everywhere.
Also, why doesn't Notepad++ work without the start command?
(I did this workaround by putting the .exe in C:\ and then simply calling C:\Anywhere> /myprogram but I'm still curious about the above.)
To get the indicated scenario where
you can start applications from anywhere using the start command (or the windows Run dialog) without including its parent folder in path variable,
but you can not start the application without the start command from any directory whithout including the full path to reach it (it is not in the path) or being located in the adecuated directory
the applications are included in the registry under the key
HKEY_CLASSES_ROOT\Applications
note: It is a "merged" view showing the combined contents of
HKEY_LOCAL_MACHINE\Software\Classes\Applications
HKEY_CURRENT_USER\Software\Classes\Applications
If you are not administrator to change the local machine configuration, you can always modify your user registry information to include the applications you need.
edited There is a second place in registry that will allow to include an application in the registry to be executed using start command, Run dialog or from anything that uses the ShellExecute or ShellExecuteEx API calls.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\

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.

PowerShell: Directory Retrieval and syntax error

A little background...I use Windows XP, Vista, and 7 quite frequently. As such, I constantly have to move my program settings from the %appdata% folder on each PC to the next. I figured that making a PowerShell script to do this for me and remove the folders after I finish would be something to ease my troubles. As I generally have my work on a flash drive, I was hoping to use relative paths, but it seems to be causing me a bit of trouble, but the biggest problem is that I don't seem to understand Powershell enough to know what mistake I'm making and how to fix it... So I came here.I figured that I could separate the task into two scripts; one for placing the directories and the second for copying them back to the original folder and removing any trace of them behind. I'll show you want I have so far. I figured retrieving them might be more difficult so I started there. Here's what I have so far. I'm using a txt file to make it easy to update the list of folders I want or need transferred so it's also being targeted by a variable.
$fldrtxt = Get-Content .\FolderList.txt
$dirget = -LiteralPath ="'%appdata%'\$_fldertxt"
$dirpost = "./Current"
# get-command | Add-Content .\"$today"_CommandList.txt
Set-Location c: {get-content $_dirget} | %{ copy-item $_dirpost}
I can't get PowerShell to recognize the same command that I use when I use the run utility. Since I'm sure I can use %appdata% to reference where I want the folders taken from and to, how can't I write this script to do what I want? I can use an absolute path, because I'd have to use a separate script for all three computers. And that I don't want.
How can I use PowerShell to do what I want and target the folders I need to use?
First: Accerss the Environment
Since I'm sure I can use %appdata% to reference where I want the folders take from and too
Wrong syntax for PowerShell, the %var% syntax for environment variables is specific to cmd scripts (and carried forward from MS-DOS batch files).
In PowerShell to access environment variables prefix their name with env:, so $env:AppData.
$_dirget = "$env:AppData\$_fldertxt"
Second: Passing parameters
Don't include the parameter name in the variable, a variable passed to a cmdlet will be passed as an argument not a parameter name. You need:
get-content -LiteralPath $_dirget
(There is something call "splat" that allows you to use a hash tables of parameter name-argument pairs as a hashtable, but that's unnecessary here.)

attempting to assign alias to path of an exe file in dos shell

I want to set an alias to my installation of firefox so I can easily start a web page, the problem is that I dont want the script to be system dependent.
Namely I want it to be able to run on a linux distribution where the command to start firefox is already mapped to 'firefox' and can easily be run that way through bash, but on my windows machine I cant seem to get it to assign to the same variable.
I saw that I could set it to '%firefox%' via the set command but that's not quite what I want.
I believe creating aliases is possible on a windows environment because the version of svn that I use auto-installed and was able to assign itself to 'svn'. Anyone know what was involved in them being able to get their alias working, or a similar way to alias a command?
If you include your Firefox path in the %PATH% environment variable, you can start FF with "firefox". Under Windows, you should edit the system-wide settings (see this link).
AFAIK, there is nothing similar to aliases under DOS/Windows (except the %firefox% way you mentioned, too). The 'svn' command you talked about most likely is the same thing, a 'svn.exe' and its path included to %PATH%.
This is a bit restrictive, as you can only use the original filename to launch a program, but you can work around this by creating a batch file in the program's path that launches the program, f.e. a FF.BAT that contains "firefox %1".
Alternatively, you can place a batch file in a path that already is in %PATH%, f.e. the Windows directory. That way, you don't have to modify %PATH%.

Resources