How to open a folder in PowerShell?
I am not talking on how to start PowerShell in a specific folder.
What I mean is that in the command line of powershell i would like
to open a folder e.g: " open document"
Use the Invoke-Item cmdlet, or its alias: ii.
PS> ii c:\windows # open the windows directory in windows explorer
PS> ii c:\book.xls # open book.xls in Excel
PS> ii . # open the current directory in windows explorer
For Powershell and cmd compatible way ( and I think the most common way):
start .
start c:\
To open the current folder within the powershell type:
PS>> explorer.exe $(pwd)
Use Invoke-Item, alias ii:
ii d:\temp\
you can use the explorer.exe to open the folder:
explorer.exe c:\temp\
explorer.exe <YourFolderPathHere>
Just to add as well to the mix:
PS C:\> ii -path c:\directory\directory\directory
If your file name has two words with a separation consider single quotation marks:
PS C:\> ii -path 'c:\directory\directory\directory directory\'
The following work [note -path is optional]
1. ii or invoke-item
2. explorer.exe
3. start
I realize the question is old but folks finding this via google may find this useful even now:
I created a cmd script with:
#REM Open directory
#REM Version 1.0
#echo off
if [%1]==[] (powershell ii .
) Else (
powershell ii %1
cd %1
)
This will also open a document such as a text file or a MS Word document, as well as opening a folder.
Putting a dot after explorer.exe will open the current directory:
explorer.exe .
I don't exactly understand what you want, but I have two possible solutions:
explorer .\Documents
or
cd .\Documents
As 'open .' in mac will open the current directory, 'start .' in window PowerShell will do the same.
another variant
hh c:\
hh http://stackoverflow.com/questions/8471106
hh $env:windir\help\WindowsPowerShellHelp.chm
Related
I am trying to use a script that I've used before in Powershell, that is supposed to copy and paste all the file names inside a folder, into a text file.
I was able to use this script on a different computer last week, but can't do it now on my laptop.
Is there something I am not aware of?
The error it's giving is
Cannot find path 'C:\b' because it does not exist.
I tried removing the /b and I got a list of all the files but with other extra data like mode, last time write, length, name, and the extension of each file.
I really need the plain file name only. How can I do this? Thanks in advance!
the /b flag only works in cmd, not PowerShell
So you could try
cmd
dir /b
exit
Which will open CMD in your terminal and execute dir /b as a normal cmd command rather than PowerShell
Alternatively, just use PowerShell's Get-ChildItem (or gci for short)
To get just the plain file names using Get-ChildItem, you can do something like this:
# Assign the folder items to a variable $x
$x = gci
# Get only the names of those items
$x.name
See SS64 for details on CMD's dir and PowerShell's Get-ChildItem
I'm wondering how can I escape the following command from PowerShell so that it works?
PS C:\Users\buster\Documents\> find -name \*.c
PowerShell says: error not found *.c
PS C:\Users\buster\Documents\> find -name *.c
PowerShell says: error not found *.c
If you used find like that (without the full path) you most likely used the find.exe that ships with Windows (C:\Windows\system32\find.exe), which is more akin to grep than to Unix find. You get that behavior because Windows searches all directories in $env:PATH for files with the given name (and one of the extensions listed in $env:PATHEXT if no extension was specified), and executes the first match. Since %windir%\system32 is usually at the beginning of the PATH, executables from there take precedence.
You could add C:\msys64\msys64\usr\bin to the beginning of the PATH (before %windir%\system32), although I wouldn't recommend that. A better way would be to define an alias for the command:
New-Alias -Name 'find' -Value 'C:\msys64\msys64\usr\bin\find.exe'
Aliases take precedence over files. You could put the alias definition in your PowerShell profile so that it's automatically loaded whenever you start PowerShell.
Or you could simply use ls -r -fi '*.c' (short for Get-ChildItem -Recurse -Filter '*.c'), which would be the PowerShell way.
Ok false alarm..
Apparently its a case of windows having an executable with the same name as msys2's find.exe under c:\windows\system32 and the windows command getting higher priority in the path list. After explicitly typing out the full path to the msys64 version of find.exe it works.
PS C:\Users\buster\Documents\> C:\msys64\msys64\usr\bin\find -name \*.c
Also, Turns out there's a better way to find *.c files native to cmd.exe that you can call from powershell like this:
PS C:\Users\buster\Documents\> cmd /c dir /S /B *.v
Using Powershell in Windows 10. To change the prompt from:
PS C:\Users\b.HQ\Desktop\tsdev\my_folder>
PS my_folder> tsc
I used the following command in Powershell:
function prompt {'PS ' + $(Get-Location | Split-Path -Leaf) + ">"}
But, each time I restart Powershell, I have to reenter this. Is there any way to persist this change?
P.S. I know nothing about the config of Powershell, and I have looked for a solution, but apart from the prompt I am using, I did not see a way of saving it.
Run powershell as administrator, then run the following:
Test-Path $Profile
if it returns false then no you don’t have a profile file yet, so create it:
New-Item –Path $Profile –Type File –Force
(this will create profile file, or will overwrite the existing one)
Then, edit your profile file:
notepad $Profile
put your function in the file and save.
I created this neat prompt to that shows the drive and last folder.
For you example it would render as
PS C:\Users\b.HQ\Desktop\tsdev\my_folder>
as
PS C:\...\my_folder>
The prompt function is:
function prompt {"PS " + (get-location).drive.name+":\...\"+ $( ( get-item $pwd ).Name ) +">"}
I built a script, that searches through all directories recursively with Get-ChildItem. The problem is, there exist directories with blank names (done with alt+255).
When the script encounters such a directory, it still lists the files in this directory, but does not search in its sub-directories.
I don't think it is possible in powershell. but you can skip to cmd and use
cmd -c dir $Location /s
that works!
As #Bert Levrau mentioned above you can do a recursive search in CMD. Using Get-ChildItem in Powershell with a folder that has an ALT + 255 name will throw it into an infinite recursive loop. You can invoke a CMD process from Powershell though using the following example: $result = cmd /c $directoryPath /s
At that point, you can work through the result to find the information that you need.
How can I open up a file in Notepad++ from the Powershell command line?
Inside PowerShell I can simply use the start and get general results
to open a python file with notepad++ here is what I did.
Start notepad++ ex1.py
this will start notepad++ and load the file ex1.py assuming you are in the same directory as the .py file. You can change that by adding the full path name
start notepad++ c:\users\you\desktop\files\ex1.py
Because the default path contains spaces, you have to quote the path to the exe. However because PowerShell is also a scripting language. A string by itself is simply evaluated as a string e.g.:
C:\ PS> 'Hello world'
Hello world
So you have to tell PowerShell you want to invoke the command that is named by the string. For that you use the call operator & e.g.:
C:\ PS> & 'C:\Program Files (x86)\Notepad++\notepad++.exe'
or if notepad++ is in your path:
C:\ PS> notepad++
or if you're in the same dir as the exe:
C:\ PS> .\notepad++
To open Notepad++ with and create a new empty file in the current path
start notepad++ newFile.txt
To open Notepad++ with an existing file
start notepad++ apples.txt
To specify the path and open multiple files
start notepad++ fruits/apples.txt, fruits/oranges.txt, package.json
To extrapolate on the previous answers and tie them up in a tidy bow:
If you want to open a file with spaces in the path or name:
. 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
or
& 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
It can also be set it as an alias:
Set-Alias -Value 'C:\Program Files (x86)\Notepad++\notepad++.exe' -Name 'NotePad'
$FileWithSpaces = 'C:\T e m p\File With Spaces.txt'
NotePad $FileWithSpaces
The top line here can be copied into (one of) your $Profile .ps1 file(s) so you don't need to keep using Set-Alias in every new PS instance.
Edit your profile and add an alias
Set-Alias -name 'npp' -value 'C:\Program Files\Notepad++\notepad++.exe'
Then:
npp c:\temp\test.txt
Edit your profile:
npp $profile
etc
I know this is an old question, but I found a bit of a workaround, quite by accident, and it is extremely straightforward. If you install and maintain Notepad++ via Chocolatey (think apt-get for Windows, but built on top of NuGet), then you get a shim that can be invoked from the command line.
cinst notepad++
And even if you already have an existing installation of Notepad, you can still "install" it from Chocolatey, and it will pull in the existing installation and maintain it.
I use Chocolatey for as much as I possibly can, because you can update everything in one fell swoop.
After that, editing things from PowerShell is a snap. Like my PowerShell profile:
notepad++ $PROFILE
Hope this helps someone, or several someones!
In my case, I wanted to start Notepad++ with a file as an argument, and open as admin. I wanted to open one of the PowerShell profiles. I had to use the following command variation:
start-process -Verb runas -filepath "C:\Program Files (x86)\Notepad++\notepad++.exe" "`"$($PROFILE.AllUsersAllHosts)`""
All the other variations didn't work, I think due to a space in the path of the file to be opened. So, you must escape the " as:
"He said `"This is fun.`""