Cannot find file path error for dir /b - Copying all file names script - powershell

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

Related

Is there a PowerShell equivalent to the dir /w of the old Windows command prompt?

In the older Windows command prompt dir /w formatted the directory output in wide format, providing a more at-a-glance view of contents to pick out folder names without having to scroll through the larger output that happens when they are all stacked vertically. This is especially useful when in VS Codes integrated terminal where the terminal window size is often restricted. Does PowerShell have an equivilent?
are you after this?
Get-ChildItem | Select-Object -Property Name
It should be an equivalent of "dir /w". If I remember correctly.
cmd /r dir /w as suggested by DSSO21 in the Comments above gives me the result I was expecting, albeit a more verbose command than I'd like.

Powershell Set-Location Auto truncate spaces error

I want to write a batch file named install.bat that will run a Powershell script file install.ps1 as Administrator. The content of the batch file install.bat as simple as below:
#echo off
PowerShell -NoExit -Command "Start-Process PowerShell -Verb RunAs '-NoExit -Command "Set-Location ''%cd%''; .\install.ps1"'"
In this batch script, I call a Powershell to run the Start-Process command. This Start-Process command would invoke another Powershell as Administrator. The second Powershell would run Set-Location command to set working directory to %cd% then invoke the install.ps1 script. An error happens with the Set-Location command if i run the install.bat file from a folder with more than 1 consecutive space characters in its name, for example:
D:\New folder
install.bat script will run normally with folder has 1 space character in its name, for example:
D:\New folder
but if a folder has more than 1 consecutive space character like:
D:\New folder
then Set-Location will searching for folder:
D:\New folder
and will show this error incase D:\New folder does not exist:
Can anyone explain this error and give me a solution?
The first thing that I looked with your script was the various control characters (single-quotes, double-quotes, etc.). Nothing obvious stood out.
I looked for other examples, but double-spaces in path names is not necessarily a common occurrence.
So I poked around and did some tests in a Command Prompt box, created a comparable test directory, and checked to see what outputs I could get.
First I sent the Current Directory variable to a PowerShell command to echo the value:
C:\temp\test\test 123>powershell -Command "Write-Host %cd%"
The output had stripped one of the space characters from the path name as you had described (double-quotes added for clarity):
"C:\temp\test\test 123"
Then I tried it with the variable within single-quotes:
C:\temp\test\test 123>powershell -Command "Write-Host '%cd%'"
This output preserved the space characters of the path name correctly (double-quotes added for clarity):
"C:\temp\test\test 123"
Then I tried it with the variable within two pairs of single-quotes as you had used in your script:
C:\temp\test\test 123>powershell -Command "Write-Host ''%cd%''"
This output also stripped one of the space characters from the path name, and it also added a space character to the front of the returned value (double-quotes added for clarity):
" C:\temp\test\test 123"
So it seems that your script might get corrected by changing to one pair of single-quotes surrounding the Current Directory variable call instead of the two pairs of single-quotes.
I'm not sure if you had another purpose for using the two pairs of single-quotes, and I suppose that most processes might not care about the extra leading space on the value return.
I hope that this helps with your script.

Dealing with blank folder names while using get-childitem in powershell

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 detect whether or not I am in powershell from a command line?

I am creating a standard windows BAT/CMD file and I want to make an IF statement to check whether or not this CMD file is run from PowerShell. How can I do that?
Edit: My underlying problem was that test.cmd "A=B" results in %1==A=B when run from CMD but %1==A and %2==B when run from PowerShell. The script itself is actually run as an old Windows Command line script in both cases, so checking for Get-ChildItem will always yield an error.
One way, it to see what your process name is, and then check its attributes:
title=test
tasklist /v /fo csv | findstr /i "test"
As long as you use a unique name in place of Test, there should be little room for error.
You will get back something like:
"cmd.exe","15144","Console","1","3,284
K","Running","GNCID6101\Athomsfere","0:00:00","test"
When I ran the above code from a bat file, my output was:
"powershell.exe","7396","Console","1","50,972
K","Running","GNCID6101\Athomsfere","0:00:00","
A potentially simpler approach that may work for you. If not, it may be suitable for others.
Create 2 separate script files: test.ps1 and test.cmd
Don't include extension when calling the script. I.e. call as <path>\test (or just test if folder is in the path environment variable.
This works because CMD prioritises which script to execute as: .bat > .cmd, whereas Powershell prioritises: .ps1 > .bat > .cmd.
The following is the output of a CMD session:
C:\Temp>copy con test.cmd
#echo cmd^Z
1 file(s) copied.
C:\Temp>copy con test.ps1
Write-Output "ps1"^Z
1 file(s) copied.
C:\Temp>.\test
cmd
C:\Temp>
And calling test from Powershell:
PS C:\Temp> .\test
ps1
PS C:\Temp>
Couldn't you try to execute a Get-ChildItem and then check %ERRORLEVEL% to see if it returns an exe not found?
http://ss64.com/nt/errorlevel.html

Open Notepad++ from PowerShell

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.`""