用于运行cmd.exe内置函数的PowerShell别名语法? [英] PowerShell alias syntax for running a cmd.exe builtin function?

查看:153
本文介绍了用于运行cmd.exe内置函数的PowerShell别名语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我最近才从 cmd.exe 切换到PowerShell,所以我经常发现通过调用以熟悉的方式完成小事情很方便cmd 来完成它们。例如,要做一个裸露的文件列表,效果很好:

As I have only recently switched to PowerShell from cmd.exe, I often find it convenient to do little things in a familiar way by calling cmd to do them. For instance, to do a 'bare' file listing this works great:

PS> cmd /c dir /b
dir1
dir2
file1.txt



<我想为此起一个别名,但是我找不到正确的语法。到目前为止,我已经尝试过:

I'd like to make an alias for this but I can't figure out the right syntax. So far I've tried:

PS> Set-Alias dirb cmd /c dir /b        # error (alias not created)
PS> Set-Alias dirb "cmd /c dir /b"      # fail (alias doesn't work)
PS> Set-Alias dirb "cmd `"/c dir /b`""  # fail (alias doesn't work)

有什么建议吗?我正在寻找一种调用内置 cmd.exe 命令(例如 dir )的常规解决方案。我也想知道如何使用PowerShell cmdlet以正确的方式产生裸露的输出,但这是目前的第二个问题。这个问题是关于从别名调用 cmd.exe 的正确语法的。

Any suggestions? I'm looking for a general solution to calling builtin cmd.exe commands (such as dir). I'd also like to know how to produce bare output the right way using PowerShell cmdlets, but that's a secondary concern at the moment. This question is about the proper syntax for calling cmd.exe from an alias.

推荐答案

我相信您想要的是一个函数,而不是别名。例如:

I believe what you want is a function, not an alias. For instance:

  function dirb {
    cmd /c dir $args[0] /b
     }

在PS提示符下,运行记事本$ profile,将其粘贴到您的个人资料中,然后它将自动加载当您打开PS控制台时,可以执行以下操作:

From a PS prompt, run notepad $profile, paste that into your profile and then it will load automatically when you open a PS console and you can do this:

dirb c:\somedir

dirb c:\somedir

请参见get -help about_functions获取有关函数的更多信息。

See get-help about_functions for more information about functions.

这篇关于用于运行cmd.exe内置函数的PowerShell别名语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆