PsIsContainer的别名?

Sab*_*ncu 3 powershell

我希望能够使用"dir"或"folder"字符串,而不是键入PsIsContainer.在PowerShell中是否有一种方法允许我将一个字符串替换为另一个字符串,如本例所示?

Kei*_*ill 7

认为你可以通过预定义几个脚本块来接近你所做的事情,例如:

$IsDir = {$_.PsIsContainer}
$IsFile = {!$_.PsIsContainer}
dir | Where $IsDir
dir | Where $IsFile
Run Code Online (Sandbox Code Playgroud)

PowerShell V3的好消息.本机支持这一点,例如:

dir -directory
dir -ad
dir -file
dir -af
Run Code Online (Sandbox Code Playgroud)


jon*_*n Z 5

您可以System.IO.FileInfo使用以下文件和Update-TypeDatacmdlet更新类型的 TypeData 。

D:\fileinfo.ps1xml

<?xml version="1.0" encoding="utf-8" ?>
<Types>
    <Type>
        <Name>System.IO.FileSystemInfo</Name>
        <Members>
            <ScriptProperty>
                <Name>dir</Name>
                <GetScriptBlock>
                 $this.psiscontainer
                </GetScriptBlock>
            </ScriptProperty>
        <ScriptProperty>
                <Name>file</Name>
                <GetScriptBlock>
                 ! ($this.psiscontainer)
                </GetScriptBlock>
            </ScriptProperty>
        </Members>
     </Type>
</Types>
Run Code Online (Sandbox Code Playgroud)

更新类型数据:

update-typedata D:\fileinfo.ps1xml
Run Code Online (Sandbox Code Playgroud)

现在您将能够输入:

gci | ?{$_.dir}
Run Code Online (Sandbox Code Playgroud)

gci | ?{$_.file}
Run Code Online (Sandbox Code Playgroud)