Run Batch with Drag-and-Drop Parameter in Powershell

Multi tool use
I have a batch program where I can drag-and-drop a file from my pendrive, and it will copy it onto my computer. I want to create a function in PowerShell (that I will save to the $profile
document) so that I can do this in powershell
>>copy-from-pendrive "file-path"
Where "file-path"
is the path to the file I want to copy.
I know that I can start batch files with start batch.bat
, but how can I pass a parameter in it?
Here is the code for the batch file
@echo off
if [%1] EQU (set /p from="From: ") else (set from=%1)
set from=%from:"=%
set from=%from:E:Elliot=%
echo from E:Elliot%from% to D:UsersElliot%from%" & echo.
xcopy "E:Elliot%from%" "D:UsersElliot%from%" /i /s /e /y
echo.
powershell batch-file drag-and-drop
add a comment |
I have a batch program where I can drag-and-drop a file from my pendrive, and it will copy it onto my computer. I want to create a function in PowerShell (that I will save to the $profile
document) so that I can do this in powershell
>>copy-from-pendrive "file-path"
Where "file-path"
is the path to the file I want to copy.
I know that I can start batch files with start batch.bat
, but how can I pass a parameter in it?
Here is the code for the batch file
@echo off
if [%1] EQU (set /p from="From: ") else (set from=%1)
set from=%from:"=%
set from=%from:E:Elliot=%
echo from E:Elliot%from% to D:UsersElliot%from%" & echo.
xcopy "E:Elliot%from%" "D:UsersElliot%from%" /i /s /e /y
echo.
powershell batch-file drag-and-drop
1
Withset from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it ishow can I pass a parameter in it
)?
– double-beep
Dec 31 '18 at 18:54
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could dostart batch.bat
and specify the file path as the%1
parameter in line 2 of the batch program. And I want to do this in powershell.
– ElliotThomas
Dec 31 '18 at 19:00
To trim (outer) double quotes from an argument use the~
modifier.if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01
add a comment |
I have a batch program where I can drag-and-drop a file from my pendrive, and it will copy it onto my computer. I want to create a function in PowerShell (that I will save to the $profile
document) so that I can do this in powershell
>>copy-from-pendrive "file-path"
Where "file-path"
is the path to the file I want to copy.
I know that I can start batch files with start batch.bat
, but how can I pass a parameter in it?
Here is the code for the batch file
@echo off
if [%1] EQU (set /p from="From: ") else (set from=%1)
set from=%from:"=%
set from=%from:E:Elliot=%
echo from E:Elliot%from% to D:UsersElliot%from%" & echo.
xcopy "E:Elliot%from%" "D:UsersElliot%from%" /i /s /e /y
echo.
powershell batch-file drag-and-drop
I have a batch program where I can drag-and-drop a file from my pendrive, and it will copy it onto my computer. I want to create a function in PowerShell (that I will save to the $profile
document) so that I can do this in powershell
>>copy-from-pendrive "file-path"
Where "file-path"
is the path to the file I want to copy.
I know that I can start batch files with start batch.bat
, but how can I pass a parameter in it?
Here is the code for the batch file
@echo off
if [%1] EQU (set /p from="From: ") else (set from=%1)
set from=%from:"=%
set from=%from:E:Elliot=%
echo from E:Elliot%from% to D:UsersElliot%from%" & echo.
xcopy "E:Elliot%from%" "D:UsersElliot%from%" /i /s /e /y
echo.
powershell batch-file drag-and-drop
powershell batch-file drag-and-drop
edited Dec 31 '18 at 18:58
ElliotThomas
asked Dec 31 '18 at 18:44
ElliotThomasElliotThomas
217
217
1
Withset from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it ishow can I pass a parameter in it
)?
– double-beep
Dec 31 '18 at 18:54
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could dostart batch.bat
and specify the file path as the%1
parameter in line 2 of the batch program. And I want to do this in powershell.
– ElliotThomas
Dec 31 '18 at 19:00
To trim (outer) double quotes from an argument use the~
modifier.if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01
add a comment |
1
Withset from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it ishow can I pass a parameter in it
)?
– double-beep
Dec 31 '18 at 18:54
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could dostart batch.bat
and specify the file path as the%1
parameter in line 2 of the batch program. And I want to do this in powershell.
– ElliotThomas
Dec 31 '18 at 19:00
To trim (outer) double quotes from an argument use the~
modifier.if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01
1
1
With
set from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it is how can I pass a parameter in it
)?– double-beep
Dec 31 '18 at 18:54
With
set from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it is how can I pass a parameter in it
)?– double-beep
Dec 31 '18 at 18:54
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could do
start batch.bat
and specify the file path as the %1
parameter in line 2 of the batch program. And I want to do this in powershell.– ElliotThomas
Dec 31 '18 at 19:00
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could do
start batch.bat
and specify the file path as the %1
parameter in line 2 of the batch program. And I want to do this in powershell.– ElliotThomas
Dec 31 '18 at 19:00
To trim (outer) double quotes from an argument use the
~
modifier. if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01
To trim (outer) double quotes from an argument use the
~
modifier. if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01
add a comment |
1 Answer
1
active
oldest
votes
You can use the param feature to require a parameter of a file path, then use Copy-Item the same as you do in the batch file:
param(
[parameter(Mandatory=$true)] [String]$path
)
copy-item -path $path -destination 'D:UsersElliot'
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.
– ElliotThomas
Dec 31 '18 at 20:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990506%2frun-batch-with-drag-and-drop-parameter-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the param feature to require a parameter of a file path, then use Copy-Item the same as you do in the batch file:
param(
[parameter(Mandatory=$true)] [String]$path
)
copy-item -path $path -destination 'D:UsersElliot'
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.
– ElliotThomas
Dec 31 '18 at 20:01
add a comment |
You can use the param feature to require a parameter of a file path, then use Copy-Item the same as you do in the batch file:
param(
[parameter(Mandatory=$true)] [String]$path
)
copy-item -path $path -destination 'D:UsersElliot'
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.
– ElliotThomas
Dec 31 '18 at 20:01
add a comment |
You can use the param feature to require a parameter of a file path, then use Copy-Item the same as you do in the batch file:
param(
[parameter(Mandatory=$true)] [String]$path
)
copy-item -path $path -destination 'D:UsersElliot'
You can use the param feature to require a parameter of a file path, then use Copy-Item the same as you do in the batch file:
param(
[parameter(Mandatory=$true)] [String]$path
)
copy-item -path $path -destination 'D:UsersElliot'
answered Dec 31 '18 at 19:09
shawnshawn
1665
1665
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.
– ElliotThomas
Dec 31 '18 at 20:01
add a comment |
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.
– ElliotThomas
Dec 31 '18 at 20:01
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:
function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.– ElliotThomas
Dec 31 '18 at 20:01
I hadn't though about just making a powershell function to do what my batch file did. Thanks for the suggestion. I ended up making the function like this:
function copy-from-pendrive ($path) { $destination = $path.Trim("E:Elliot") copy-item -Force -Recurse -Verbose -Path "$path*" -Destination "D:UsersElliot$destination" }
to get everything working the way I wanted it to.– ElliotThomas
Dec 31 '18 at 20:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990506%2frun-batch-with-drag-and-drop-parameter-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
k g,2,un4,6lX,SuE0P,dYPKk C
1
With
set from=%from:"=%
you want to remove surrounding quotes from this variable? But what is your question and what has to with PowerShell (if it ishow can I pass a parameter in it
)?– double-beep
Dec 31 '18 at 18:54
@double-beep Yes, sorry. I forgot the question mark there. My question is how I could do
start batch.bat
and specify the file path as the%1
parameter in line 2 of the batch program. And I want to do this in powershell.– ElliotThomas
Dec 31 '18 at 19:00
To trim (outer) double quotes from an argument use the
~
modifier.if "%~1"=="" (set /p from="From: ") else (set "from=%~1")
– LotPings
Dec 31 '18 at 19:01