Windows Powershell Basic Questions - new user - powershell

When trying to open a file with text editor VIM, I am unable to open the file unless VIM (shortcut) is in my current working directory. As an example, I am able to write start firefox to open a firefox window. However, start vim C:\filepath\filename.txt does not work unless a vim shortcut is in my current directory. How do I get around this?
Also, is there a way to have a program execute a file in the current working directory without having to reference the entire file path? For example instead of Start-Process vim C:\Users\User\Desktop\File\file.txt is there an available path shortcut like Start-Process vim ~\file.txt with ~ representing the current working directory?

The OS need to determine the full path of the exe, no matter what.
There's 2 ways that it will happen.
You're calling the executable from it's working directory
The executable location is in the Windows environment variable.
You can view the PATH variable content through this simple statement
$env:Path -split ';' | sort
You sill see that the Firefox path is listed there, but not the one from VIM.
That's why the former can be started by it's executable name and the latter require the full path.
You need to add VIM directory to your PATH variable if you want to be able to call it just by typing vim
Otherwise, if you have restricted access or don't want to edit that variable, you can also set a $vim variable, then invoke it whenever you want to call the executable.
Regarding the second part of your question
Powershell use the dot as a reference to the current directory .\file.txt.
You can also just specify the filename without anything else file.txt.
Both backslash \ & slash / work for filepath so .\file.txt and ./file.txt are both valid ways to reference the file.
Use ..\ to reference the parent directory (e.g. ..\file.txt)
$Vim = "c:\Path\To\Vim.exe"
& $vim "file.txt"
& $vim ".\file.txt"
#Forward slash also work for paths
& $vim "./file.txt"

Related

Display a nicely formatted path in PowerShell have code how to format for profile

I found a snippet of code that if I paste in PowerShell It displays all of my windows path variables on one line. What would the syntax be for adding this code to my profile?
Push-Location env:
(ls path).value.split(";")
Pop-Location
Put this function in your PowerShell profile:
function Get-Path {
$env:Path.Split(";")
}
After this function is defined, you can type Get-Path to see the list of directories in your Path.
If you don't already have a profile script, run the command help about_Profiles for more information.
Not sure why you'd prefer to see PATH variable on one line, but here's the code to do it.
C:>(ls Env:\Path).value
I prefer separate lines:
C:>(ls Env:\Path).value.split(';')
As far as your PowerShell Profile goes, open PowerShell and run:
C:>$profile
It will tell you the path to your profile (make the file and directory if it doesn't exist).
Then, copy + paste the code above into it.
It will run whenever you open powershell.

Creating files at PSModulePath in batch

I am currently trying to write a batch program that installs a module named SetConsolePath.psm1 at the correct location. I am a beginner with Batch and I have absolutely no powershell experience.
Through the internet, I have learned how to display PSModulePath with powershell -command "echo $env:PSModulePath.
How can I, via .bat file, move SetConsolePath.psm1 from the desktop to the location displayed by powershell -command "echo $env:PSModulePath?
Thank you in advance, and I apologize for my lack of experience.
Before I answer, I must out that you do not want to copy PowerShell module files directly to the path pointed by PsModulePath. You really want to create a folder inside PSModulePath and copy the files there instead.
The prefix env in a Powershell variable indicates an environment variable. $env:PSModulePath is actually referring to the PSMODULEPATH environment variable. On the command line, and in batch files, environment variables can be displayed by placing the name between percent symbols. (In fact, you could have displayed this value by typing echo %PSMODULEPATH% instead.)
To reference the desktop folder, have a look at this answer, which shows you how to use another environment variable, USERPROFILE.
Therefore, to copy the file from the desktop directory to the path specified in PSModulePath, you would do this:
COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%"
And, as I warned earlier, you really should copy the file to a folder underneath PsModulePath. So what you really want is:
IF NOT EXIST "%PSMODULEPATH%\MyNewFolder" MKDIR "%PSMODULEPATH%\MyNewFolder"
COPY "%USERPROFILE%\Desktop\SetConsolePath.psm1" "%PSMODULEPATH%\MyNewFolder"

Open a file with spaces in the pathname

I'm trying to use vscode (0.4.0) to open a file from the powershell commandline which has spaces in the path. Specifically the powershell profile, which lives under "My Documents"
I initially tried just:
code $PROFILE
but this just opens a file called "My" and does similar if I quote the file path:
code '\\myserver\myname\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
code "\\myserver\myname\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
how do I go about opening a file from the commandline when it has spaces in the path?
I have also tested it on a local file "C:\temp\Test Spaces\foobar.txt" and it does similar, so it's not related to UNC
This seems strange, but I tried double quotes, and it worked:
code ""C:\test\test file.js""

'findstr' is not recognized as an internal or external command,

I got the following error while starting JBoss from a command line prompt today:
'findstr' is not recognized as an internal or external command
Please google it, you can find a lot of answers. But do as below to fix it. Add the following value to Right Click My Compuer -> Advanced -> Environment Variables -> System Variables -> Select Path variable -> append the below value.
C:\WINDOWS\system32
It should work with that change.
As others pointed, issue is in wrong settings of PATH variable in Windows.
According to article this is most probably because some stupid installer wrongly modified PATH variable in Windows registry. Registry has 2 different string value types - REG_SZ and REG_EXPAND_SZ. Only the second one allows for expansion of %SystemRoot%.
So check your path by typing set path in command prompt. If you see unexpanded %SystemRoot% and other variables in Path, you are affected (PATH should show only plain directory names, not variables).
You need to edit Path variable in registry: HKEY_CURRENT_USER\Environment and HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. As it is not possible to change the type of key, save the path value somewhere, delete the key and re-create it with type REG_EXPAND_SZ. You need to logout for changes to take effect.
for me it works when I've coped findstr(from windows/system32) to wildfly/bin
Please go throught the simplest steps:-
go to C:\Windows\system32\ and copy findstr.exe file.
paste this file into the location C:\Program Files\Java\jdk1.6.0_24\bin
Run your jboss again you will get out of this.....
Check to see if you %SystemRoot% is evaluating (type set path into a command prompt, you should not see %SystemRoot%, but instead that actual path). If your path variable's (user, or systems) first entry begins with an %(an environment variable) this can cause an issue.
To resolve this, simply swap this first entry with anything else in your path that does not lead with an environment variable.
You can also hard code the directory by replacing 'findstr' with 'C:\Windows\system32\findstr'. This is useful when using systems with restricted user permissions.
I have try to work with play framework but stuck with to run activator.bat file but solution is the same just copy file from windows/system32/findsr and past it to under stuck folder then run the respective file again.
thanks to andrewsiand Suryaprakash
Please beware that current Windows systems use a Capital "S" for the System directory, so:
C:\WINDOWS\System32
%SystemRoot%\System32
Omitting the capital S will result in a neglect of the line in the %PATH%
In my case (not JBoss related) the following helped to fixed this error.
Instead of:
SET path="%path%;C:\some\additional\path"
I used:
SET "path=%path%;C:\some\additional\path"
For IBM ACE solution for
'findstr' is not recognized as an internal or external command,
Go to the path C:\Windows\System32
Find the findstr.exe, copy it and then find the path where you bin file of your application is found. eg C:\Program Files\IBM\ACE\11.0.0.12\server\bin then past it inside the bin file
cancel the console of ace and re-open it.
Then run ACE toolkit command on ace console.
Then press enter, now it can open.

Prevent vim :make from changing the current working directory?

Synopsis:
When calling vim's make command, it changes the current working directory (cwd) to the directory of the current file. It then runs the makeprg from there. I want to prevent the make command from changing the cwd, and instead call the makeprg from the directory of the parent vim instance
Example:
I have the following standard perl project hierarchy
project/
lib/
My/
Module/
Foo.pm
My PERL5LIB is set to
PERL5LIB=':lib'
In my .vimrc I have
compiler perl
set makeprg=perl\ -c\ %
I edit my module using vim from the root project level:
/path/to/project$ vim lib/My/Module/Foo.pm
In vim :pwd works as expected:
:pwd
"/path/to/project"
Also calling !perl -c works as expected, finds my project lib, and displays the result in a shell window:
:!perl-c %
OUTPUT:
perl -c lib/My/Module/Foo.pm
lib/My/Module/Foo.pm Syntax ok
However :make returns an error
:make
"Can't open perl script lib/My/Module/Foo.pm : No such file or directory"
Setting makeprg to pwd shows the problem
:set makeprg=pwd
:make
"/path/to/project/lib/My/Module"
So before make runs makeprg it is changing to the directory of the current file, which is why perl can't find 'lib/.../Foo.pm' there.
Is there any way to prevent make from doing this?
If Vim's :make command is changing the current working directory, and autochdir is not set, a plugin may have added an autocommand to the QuickFixCmdPre set. One plugin that does this is eclim, which calls the QuickFixLocalChangeDirectory() function if g:EclimMakeLCD is set to 1.
Use :au to find all the autocommands in your current configuration, paying particular attention to entries for QuickFixCmdPre and make.