You don't need double quotes for simple string text or a simple variable.
Copy-Item -path $file_path -Destination 'C:\destination'
Double quotes are for variable expansion use cases where it is needed and in some formatting use cases. For example, combining a variable with something else, so say a file path.
Get-ChildItem -Path 'D:\Temp' |
ForEach {
$PSItem
$PSitem.BaseName
'Processing ' + $PSItem.FullName
"Processing $($PSItem.FullName)"
} |
Select -First 4 |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 06-May-20 20:30 AddressFiles
AddressFiles
Processing D:\Temp\AddressFiles
Processing D:\Temp\AddressFiles
#>
So, if I took your sample and refactor a bit so you can see what I mean:
$File_path = 'D:\Temp'
Get-ChildItem -Path $File_path -File |
Select -First 4 |
ForEach {
Copy-Item -Path $PSItem.FullName -Destination 'D:\Temp\est' -WhatIf
}
# Results
<#
What if: Performing the operation "Copy File" on target "Item: D:\Temp\(MSINFO32) command-line tool switches.pdf Destination: D:\Temp\est\(MSINFO32) comm
and-line tool switches.pdf".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\23694d1213305764-revision-number-in-excel-book1.xls Destination: D:\Temp\est\23694
d1213305764-revision-number-in-excel-book1.xls".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url Destination: D:\T
emp\est\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\abc.bat Destination: D:\Temp\est\abc.bat".
#>
Yet again, as [David C. Rankin], unless your environment is properly configured, you can only run PowerShell commands in the PowerShell consolehost, ISE or VSCode.
You can run external executables in PowerShell, but you must call it properly, especially if you are using the PowerShell ISE.
• PowerShell: Running Executables
Table of Contents
- Direct - Using the environment path or local folder
- Invoke-Expression (IEX)
- Invoke-Command (ICM)
- Invoke-Item (II)
- The Call Operator &
- cmd /c - Using the old cmd shell
- Start-Process (start/saps)
- [Diagnostics.Process] Start()
- WMI Win32_Process Create() Method
- Stop-Parsing Symbol --%
cmd.exeand as does bash. PowerShell has no clue what to do with Linux shell commands in a bash script. Unless you have bash-for-windows installed so you can invokebash someLinux.shwithin PowerShell, you will have to write an equivalent PowerShell script for what the bash script does.aliasessuch aslfordirandcpforcopy/xcopy. The options will be different. If you have MinGW installed and its path in your user environment -- then you can use the shell commands provided as part of Msys/MinGW from PowerShell. (or from the Command Prompt for that matter)$file_path(PowerShell has its own way of quoting). If sayfile_path=\"some path\to\file\", then when you double-quote"$file_path"you would end up with""some path\to\file""and splitting would occur on the space -- which could lead to yourUnexpected EOF while looking for matching ' " '. There are a number of issues that "might" apply.. You need to determine what you have installed, e.g.MinGWor bash-for-windows, or just PowerShell, and update your question.