0

Is it possible to copy files using bash script (.sh) on powershell?

Tried using cp and copy, but got command not found error. However, if use cp or copy in the powershell command line, it does work.

I tried

Copy-Item -path "$file_path" -Destination "C:\destination\"

where $file_path is a variable with the source file This resulted in syntax errors-

Unexpected EOF while looking for matching ' " '
syntax error: unexpected end of file

The exact same copy-item command works when executed in powershell command line.

4
  • 2
    Your shell IS the interpreter. PowerShell has its own language as does cmd.exe and 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 invoke bash someLinux.sh within PowerShell, you will have to write an equivalent PowerShell script for what the bash script does. Commented Aug 16, 2020 at 3:53
  • 1
    PowerShell also provide a number of Unix like aliases such as l for dir and cp for copy/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) Commented Aug 16, 2020 at 3:56
  • I am not sure about that. I am running a bash script that reads a file and prints out a particular line using if statement. It's working great, just like it would work on linux. Commented Aug 16, 2020 at 3:56
  • 1
    From your description it sounds like windows is including quotes within what is in $file_path (PowerShell has its own way of quoting). If say file_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 your Unexpected EOF while looking for matching ' " '. There are a number of issues that "might" apply.. You need to determine what you have installed, e.g. MinGW or bash-for-windows, or just PowerShell, and update your question. Commented Aug 16, 2020 at 4:02

1 Answer 1

1

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

  1. Direct - Using the environment path or local folder
  2. Invoke-Expression (IEX)
  3. Invoke-Command (ICM)
  4. Invoke-Item (II)
  5. The Call Operator &
  6. cmd /c - Using the old cmd shell
  7. Start-Process (start/saps)
  8. [Diagnostics.Process] Start()
  9. WMI Win32_Process Create() Method
  10. Stop-Parsing Symbol --%
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.