File path with quotation mark issue of Powershell - powershell

I was trying to write a script to test the availability of the file path. One of the process is that user have to input the file path or just drag the file into the Powershell command prompt.
In this process,
User will input the file path like C:\Program Files\7-Zip\7z.exe and the string will become the value of $filePath variable which will be used as the parameter of the Test-Path command.
But sometime, user will just drag the file into the Powershell command prompt so that the string will have a pair of quotation mark included just like the picture 1.
("C:\Program Files\7-Zip\7z.exe")
Picture 1
Then you will see when I try to test the path using Test-Path command with that $filePath variable while the value(string) of the $filePath included a pair of quotation mark, the result will always be False even though the path is existing and valid.
But when I use the same Test-Path command without using variable (I mean just copy and paste the file path into the command), it works normally.
IT'S WEIRD!
I have tried typing the file path by keyboard into the variable instead of dragging the file into Powershell command prompt. (Without Quotation mark)
Then use the same method to test the filepath (using variable for the file path). It works fine.
Picture 2
I don't get it. Aren't they the same thing?
when
$filePath = "C:\Program Files\7-Zip\7z.exe"
Below 2 commands SHOULD have the same result! WHY they are not?
Test-Path -Path $filePath
Test-Path -Path "C:\Program Files\7-Zip\7z.exe"

In case of drag and drop, it looks like if the path has no spaces it will return true . If it has a space then PowerShell places quotes around it. In that case, PowerShell is literally seeing the path as "C:\Program Files\7-Zip\7z.exe"
What you can do is use the -replace operator like this -
Test-Path -path ($filepath -replace '"', "") -PathType Leaf
OR
As suggested by #Josefz in the comments, you could also use Trim() method like -
Test-Path -path ($filepath.Trim('"')) -PathType Leaf
Not exactly an explanation to your problem, but you could use this as a workaround.

If the user types a filename that contains embedded " characters, then Test-Path will return $false. Why? File names cannot contain the " character; the " character is used by parsers to indicate that an argument in a string contains whitespace. So this will of course return $false:
$filePath = '"C:\Program Files\7-Zip\7z.exe"'
Test-Path $filePath
The embedded " characters are not part of the file name; you need to omit them for the result to be $true.

What version of powershell are you using? I get true for both commands
PS C:\Users> $filePath = "C:\Program Files\7-Zip\7z.exe"
PS C:\Users> Test-Path -Path $filePath
True
PS C:\Users> Test-Path -Path "C:\Program Files\7-Zip\7z.exe"
True
PS C:\Users> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 48
EDIT ----------
Got it, the problem is that Read-Host will save the string literally, with the quotes. The simple solution is to remove the quotes
PS C:\Users> $filePath = Read-Host -Prompt "enter input"
enter input: "C:\Program Files\7-Zip\7z.exe"
PS C:\Users> $filePath
"C:\Program Files\7-Zip\7z.exe"
PS C:\Users> Test-Path -Path $filePath
False
PS C:\Users> Test-Path -Path $filePath.replace("`"","")
True

Related

How can I determine the specific character file with powershell?

I have a file.
c:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx
but powershell test-path error.
PS C:\\> Test-Path -LiteralPath "C:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx" -PathType Leaf
PS C:\\> False
PS C:\\> Test-Path "C:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx" -PathType Leaf
PS C:\\> False
Does anyone have some ideas on how to go about this?
Thanks!
First off, having such file names is begging for trouble. They are much more trouble than help.
That being said, Powershell quoting rules explains how to work with quotes. Since there is a single quote in the file name, it must be escaped by doubling it - the usual backtick doesn't help here. Here-Strings work too. Like so,
# single quote twice
test-path -literalpath '[test^!#$%&''()=~{`}_+-^[];.,] test.xlsx'
True
# here-string
test-path -literalpath #'
>> [test^!#$%&'()=~{`}_+-^[];.,] test.xlsx
>> '#
True
# here-string in varialbe
$f =#'
>> [test^!#$%&'()=~{`}_+-^[];.,] test.xlsx
>> '#
test-path -literalpath $f
True

Set Location in PowerShell that contains a ampersand (&)

How do i set my directory using PowerShell to the below location ?
W:\B&M\Store Segmentation\Pete\Python
I tried using:
PS > Set-Location -Path W:\B"&"M\Store Segmentation\Pete\Python
While using " or ' is totally a better idea, but should be informed of PowerShell escape character:
`
Using this caracher, you can scape special characters, for example:
Set-Location C:\A`&B
You need it for example in cases that the folder name contains []:
Set-Location 'C:\A&B[1]' # Will Fail
Set-Location 'C:\A&B`[1`]' # Correct
Use " double quotes or ' apostrophes as follows:
Set-Location -Path "W:\B&M\Store Segmentation\Pete\Python"
or
Set-Location -Path 'W:\B&M\Store Segmentation\Pete\Python'
Read About Quoting Rules for explanation.

path as parameter powershell

I have problem with path. I need to copy all files from directory I add as 1st parameter in Powershell command.
Now I have:
Copy-Item -Path "$args[0]/" -Filter*.*
So it will copy to location I am now (and it's ok, I don't want other location) but it doesn't copy anything. Please help.
Pretty sure the issue is that the 0 element in $args is not expanding in the string creating an invalid path. Wrap the variable in $() to allow it to expand inside a double quoted string. Else you would end up trying to copy the folder C:\TEST[0]/ which obviously is not correct.
Copy-Item -Path "$($args[0])/" -Filter *.* -Recurse
Not yet sure why you have a forward slash in there since Windows pathing uses backslashes.
function Copy-Something(){
test-Path "$($args[0])/"
test-path "$($args[0])"
}
Copy-Something C:\temp
With what little you have provided the output shows that it might be redundant to have the slash there. Would also recommend calling Test-Path on the argument anyway as it might have caught this for you.
True
True
From Question Comment
You are looking for the -Recurse parameter if you also want folder contents.
From Answer Comment
If you want the contents and not the folder you should be able to do something like this:
Copy-Item -Path "$($args[0])\*" -Filter *.* -Recurse
When in doubt, Get-Help Command.
PS C:\> Get-Help Copy-Item -ShowWindow
-------------------------- EXAMPLE 3 --------------------------
This command copies the contents of the C:\Logfiles directory
to the C:\Drawings\Logs directory. It creates the \Logs
subdirectory if it does not already exist.
Windows PowerShell
PS C:\> Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse

Trim quotes from read-host directory stored as variable

I have a small Powershell script I wrote to help rename folders of files, based off the filenames in another folder.
I have two read-host lines that catch the input from the user and store the input as the source folder and destination folder as strings. This makes life easier as I can drag and drop rather than full typing the path.
The problem is that Powershell keeps throwing errors, saying it can't find the drive "X:
This seems to be being caused by the quotes around the path, as removing them after dragging and dropping works fine.
Here is how it is captured:
$source = Read-Host "Source folder"
$destination = Read-Host "Destination folder"
[array]$a = Get-ChildItem $source
[array]$b = Get-ChildItem $destination
What is the easiest way to remove the quotes from those strings, before running the Get-ChildItem command? I have tried things like $source.replace, $_. trim and also $source -replace ('"', "")
Can't seem to get this to work.
Use single quotes, like so:
PS C:\> $foo = 'x:\"some weird\path"'
PS C:\> $foo
x:\"some weird\path"
PS C:\> $foo.Replace('"', '')
x:\some weird\path
PS C:\> $foo -replace '"', ''
x:\some weird\path

How do I make a powershell function that expands filename arguments to include their paths if unqualified?

Starting up notepad++ or many other GUI applications in Windows that will accepted fully qualified filenames of documents on the command line, but which do not accept them if they are not fully qualified, is often done in DOS/Windows batch files like this:
#echo off
start "notepad++" "C:\Program Files (x86)\Notepad++\notepad++.exe" %*
The above, if saved as "npp.cmd" will let you type "npp foo.txt" and it will work.
Note that without the npp.cmd, even typing out the full path to the exe, but not fully qualifying the file to edit doesn't work, like this:
"C:\Program Files (x86)\Notepad++\notepad++.exe" foo.txt
This however, DOES work:
"C:\Program Files (x86)\Notepad++\notepad++.exe" c:\users\warren\foo.txt
A way to easily work around this limitation is to make a batch file (.cmd) as shown at the top of this file. I'm learning PowerShell and trying to find the equivalent magic to the "start .... %*" incantation in the batchfile at the top. I believe it would have to be a 'powershell function'.
Here's what I have so far:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp $args }
The above is equivalent, in the end to simply an alias, because $args is really not equivalent to %*, in that it does not do parameter expansion. I think I need something like this:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp globexpand($args) }
globexpand is of course, a placeholder, for some kind of expansion/globbing routine, which I haven't been able to find yet in PowerShell.
try this:
new-item -path alias:nppapp -value "C:\Program Files (x86)\Notepad++\notepad++.exe"
function npp { nppapp (join-path -Path $pwd -ChildPath $args[0]) }
$pwd is an automatic variable with the current working path as value
Edit:
function npp {
if ($args[0] -match '.:\\.+')
{
nppapp $args[0]
}
else
{
nppapp (join-path -Path $pwd -ChildPath $args[0]) }
}