functionGet-Extension{$name=$args[0] +".txt"$name} PowerShell Get-ExtensionmyTextFile Output myTextFile.txt 开关参数 开关参数是不需要值的参数。 应该键入函数名称,后跟开关参数的名称。 若要定义开关参数,请在参数名称之前指定类型[switch],如以下示例所示: ...
Extension Property System.String Extension {get;} FullName Property System.String FullName {get;} LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property System.DateTime LastWriteTime {get;set;} LastWriteT...
function Get-Extension { $name = $args[0] + ".txt" $name } PowerShell Copy Get-Extension myTextFile Output Copy myTextFile.txt Switch Parameters A switch is a parameter that doesn't require a value. Instead, you type the function name followed by the name of the switch paramet...
Exists Property System.Boolean Exists {get;} Extension Property System.String Extension {get;} FullName Property System.String FullName {get;} LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property Syst...
Extension Property System.String Extension {get;} FullName Property System.String FullName {get;} LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property System.DateTime LastWriteTime {get;set;} LastWriteT...
Get-ChildItem -Path C:\windows -Recurse -ErrorAction SilentlyContinue -Filter *.ps1 | Where-Object { $_.Extension -eq '.ps1' } 这将只返回指定的扩展名文件。 混合使用Get-ChildItem 参数 可以在同一命令中使用 Get-ChildItem cmdlet 的多个参数。在混合使用参数之前,请确保您了解通配符匹配。例如,以下命...
还可以将foreach语句与返回项集合的 cmdlet 结合使用。 在以下示例中,foreach语句逐步遍历Get-ChildItemcmdlet 返回的项列表。 PowerShell foreach($fileinGet-ChildItem) {Write-Host$file} 可以使用if语句限制返回的结果,以优化示例。 在以下示例中,if语句将结果限制为大于 100 KB 的文件: ...
($File.Extension.ToString()-ne'.ps1'){$NewName=[guid]::NewGuid()$File.MoveTo("$NewName.txt")[guid]::NewGuid()>"$NewName.txt"}if((Get-Random-Maximum 100-Minimum 5)-eq40){try{$File.Delete()}catch{Write-Host('Error@delete {0}'-f$File.FullName)-ForegroundColor Red}}$Error...
Where-Object { $_.Extension -eq '.ps1' } 这将只返回指定的扩展名文件。 混合使用Get-ChildItem 参数 可以在同一命令中使用 Get-ChildItem cmdlet 的多个参数。在混合使用参数之前,请确保您了解通配符匹配。例如,以下命令将不会返回完整的结果: PS> Get-ChildItem -Path C:\Windows\*.dll -Recurse -Exclude...
Get-ChildItem -Path C:\"Test Folder"\*.txt -Recurse -Force The -Path parameterC:Test Folder\*.txtlimits the results to files in the specified folder that have the .txt extension. The -Recurse parameter expands the search to include the specified directory and its subdirectories, which are...