英文:
Run a batch (.BAT) file in PowerShell
问题
回到我使用Windows 3.1的日子,我编写了两个简单的批处理文件"cc.bat"和"xx.bat",自那以后一直在使用它们。我一直将它们放在Windows路径中。
cc.bat会带我到C:驱动器,回到根目录并清除屏幕。
@echo off
c:
cd\
cls
xx.bat只是关闭DOS窗口(当时只是为了节省两次按键,我很菜...)。
@echo off
exit
今天我在PowerShell中尝试它们,第一个只清除了屏幕,第二个什么也没做?
我知道如何执行它们,但我只想输入"cc"然后运行它们。
我是否需要在脚本中添加类似"cmd.exe -/c"或"Start-Process"之类的东西,还是有像DOS中那样简单的方法?
提前感谢。
英文:
Back in my windows 3.1 days I wrote two simple batch files "cc.bat" and "xx.bat" and I've been using them since. I have always placed them in in the Windows Path.
cc.bat will take me to the C: Drive, take me to the root and clear the screen.
@echo off
c:
cd\
cls
xx.bat just closes the DOS Window (at the time just wanted to save two key strokes, I was lame...).
@echo off
exit
Today I tried them in PowerShell and the first only cleared the screen and the second did nothing?
I do know how to execute them but I just want to type "cc" and have them run.
Do I need to add something like "cmd.exe -/c" or "Start-Process" inside the script or is there a simple way like in DOS???
Thanks in advance
答案1
得分: 0
你可以使用符号链接到批处理文件(例如,将批处理文件放在某个地方,然后在$HOME/bin/cc
处创建符号链接)。
然后,你就不需要指定文件类型,只需使用cc
代替cc.bat
。
英文:
You can use a symlink to the bat file (e.g. your bat files somewhere and the symlinks at $HOME/bin/cc
)
Then you wouldn't have to specify the filetype, just use cc
instead of cc.bat
.
答案2
得分: 0
以下是翻译好的部分:
cc.bat
- 切换到 C 盘然后清屏
PowerShell 等效的函数:
<# 创建一个名为 Set-LocationThenClear 的新函数
带有一个名为 'Path' 的参数,默认值为 'C:\' #>
function Set-LocationThenClear {
param(
[Parameter(Position=0)]
[string]$Path = 'C:\'
)
Set-Location -Path $Path
Clear-Host
}
New-Alias -Name 'cc' -Value 'Set-LocationThenClear'
在这里,我创建了一个 PowerShell 函数,它可以执行您想要的操作,同时还可以接受任何您想要的路径。
用法:
# 长方式:
PS> Set-LocationThenClear -Path 'F:\'
# 使用默认值:
PS> Set-LocationThenClear
# 使用别名:
PS> cc 'F:\' # 由于 `-Path` 具有 "Position=0",我们不需要指定名称
# 使用别名和默认值:
PS> cc
xx.bat
- 退出 / 关闭终端
Powershell 中的 exit
有点奇怪,因为它只是一个具有操作的保留字。它不是 cmdlet、函数或可执行文件,它只是内置到 PowerShell 中的。因此,由于某种原因,它们不允许您创建一个别名来代替它。
因此,遵循与上述相同的步骤,我可能会执行类似以下的操作:
function Exit-Terminal {
exit
}
New-Alias -Name 'xx' -Value 'Exit-Terminal'
用法:
# 长方式
PS> Exit-Terminal
# 使用别名
PS> xx
注意:我首先创建一个带有长名称的函数,然后创建一个别名,而不是直接将函数命名为 cc
或 xx
,因为这是 PowerShell 中函数命名的最佳实践。建议您使用 Verb-Noun
格式来命名函数。
他们甚至提供了一组建议的带有含义的动词列表:Get-Verb
。
这里最重要的部分是这些只是函数,它们尚未存储在任何地方。如果您将它们粘贴到 PowerShell 中,您将能够在该会话中使用它们。但是,您无法在其他会话中使用它们,它们会在关闭窗口时消失。
要使它们持久化,我将它们放在我的 PowerShell 配置文件中。您可以通过键入以下内容来访问它:
PS> notepad.exe $PROFILE
如果它不存在,那么您可以使用 New-Item $PROFILE
,如果已存在,则会收到错误消息。
您将在此文件中放置的任何内容都将在每次打开 PowerShell 终端时运行(Windows PowerShell 和 PowerShell 使用不同的配置文件,因此请记住这一点)。
英文:
There are various ways you could do this. If it were me, this is how I would do it...it's not "simple", but it's my preferred way of adding custom functions to my terminal:
cc.bat
- change directory to C:\ then clear the screen
PowerShell equivalent as a function:
<# Creates a new function named Set-LocationThenClear
Takes one parameter named 'Path' which is defaulted to 'C:\' #>
function Set-LocationThenClear {
param(
[Parameter(Position=0)]
[string]$Path = 'C:\'
)
Set-Location -Path $Path
Clear-Host
}
New-Alias -Name 'cc' -Value 'Set-LocationThenClear'
Here I've created a function in PowerShell that does what you want, but it can also take any path you want as well.
Usage:
# Long way:
PS> Set-LocationThenClear -Path 'F:\'
# Using defaults:
PS> Set-LocationThenClear
# Using alias:
PS> cc 'F:\' # and since `-Path` has "Position=0", we don't need to name it
# Using alias and default:
PS> cc
xx.bat
- exit / close the terminal
Powershell is kinda weird with exit
because it's just a reserved word that has an action. It's not a cmdlet, or a function or a exe file, it's just built into powershell. So for some reason, they don't let you create an alias against it.
So following the same steps as above, I might do something like:
function Exit-Terminal {
exit
}
New-Alias -Name 'xx' -Value 'Exit-Terminal'
Usage:
# Long way
PS> Exit-Terminal
# Using alias
PS> xx
Note: The reason why I first create a function with a long name and then create an alias rather than just naming the function cc
or xx
directly is because that's just best practice for naming functions in PowerShell. It's suggested that you name functions with a Verb-Noun
format.
They even have a list of suggested verbs to use with meanings: Get-Verb
.
The most important part here is that these are just functions, they don't live anywhere yet. If you paste these into powershell, you'll be able to use them within that session. But you couldn't use them in other sessions, and they would go away as soon as you close the window.
To make them persistent, I put them in my PowerShell profile. Which you can access by typing in:
PS> notepad.exe $PROFILE
If it doesn't exist, then you can use New-Item $PROFILE
, if it already exists you'll just get an error.
Anything you put in this file will be run every single time you open a powershell terminal (Windows PowerShell and PowerShell use different profile files, so keep that in mind as well).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论