新聞中心
(一)查詢
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了巴林左旗免費建站歡迎大家使用!
使用 Get-ChildItem 直接獲取某個文件夾中的所有項目。 添加可選的 Force 參數(shù)以顯示隱藏項或系統(tǒng)項。為了顯示包含的項,你還需要指定 -Recurse 參數(shù)。 (這可能需要相當(dāng)長的時間才能完成。)
Get-ChildItem -Path C:\ -Force
Get-ChildItem -Path C:\ -Force -Recurse
Get-ChildItem 可以使用其 Path、Filter、Include 和 Exclude 參數(shù)篩選項,但那些通常只基于名稱。 還可以通過使用 Where-Object 基于項的其他屬性執(zhí)行復(fù)雜的篩選。
Get-ChildItem -Path $env:ProgramFiles -Recurse -Include *.exe | Where-Object -FilterScript {($_.LastWriteTime -gt '2005-10-01') -and ($_.Length -ge 1mb) -and ($_.Length -le 10mb)}
還可以使用通配符匹配進(jìn)行枚舉,Windows PowerShell 通配符表示法包括:
星號 (*) 匹配零個或多個出現(xiàn)的任何字符。
問號 (?) 完全匹配一個字符。
左括號 ([) 字符和右括號 (]) 字符括起一組要匹配的字符。
若要在 Windows 目錄中查找?guī)в泻缶Y .log 并且基名稱中正好有五個字符的所有文件,請輸入以下命令:
Get-ChildItem -Path C:\Windows\?????.log
若要在 Windows 目錄中查找以字母 x 開頭的所有文件,請鍵入:
Get-ChildItem -Path C:\Windows\x*
若要查找名稱以 x 或 z 開頭的所有文件,請鍵入:
Get-ChildItem -Path C:\Windows\[xz]*
也可以使用排除參數(shù),例如排除名稱包含9516的文件:
Get-ChildItem -Path C:\WINDOWS\System32\w*32*.dll -Exclude *[9516]*
Get-ChildItem -Path C:\Windows\*.dll -Recurse -Exclude [a-y]*.dll
包含參數(shù)和排除參數(shù)可結(jié)合使用:
Get-ChildItem -Path C:\Windows -Include *.dll -Recurse -Exclude [a-y]*.dll
(二)復(fù)制
如果目標(biāo)文件或文件夾已存在,則復(fù)制嘗試失敗。 若要覆蓋預(yù)先存在的目標(biāo),請使用 Force 參數(shù)。即使當(dāng)目標(biāo)為只讀時,該命令也有效。
Copy-Item -Path C:\boot.ini -Destination C:\boot.bak -Force
你仍然可以使用其他工具執(zhí)行文件系統(tǒng)復(fù)制。 XCOPY、ROBOCOPY 和 COM 對象(如 Scripting.FileSystemObject)都適用于 Windows PowerShell。 例如,可以使用 Windows Script Host Scripting.FileSystem COM 類將 C:\boot.ini 備份到 C:\boot.bak:
(New-Object -ComObject Scripting.FileSystemObject).CopyFile('C:\boot.ini', 'C:\boot.bak')
(三)創(chuàng)建
如果某個 Windows PowerShell 提供程序具有多個類型的項(例如,用于區(qū)分目錄和文件的 FileSystem Windows PowerShell 提供程序),則需要指定項類型。
New-Item -Path 'C:\temp\New Folder' -ItemType Directory
New-Item -Path 'C:\temp\New Folder\file.txt' -ItemType File
(四)刪除
刪除的 時候,如果不希望系統(tǒng)針對每個包含的項都提示你,則指定 Recurse 參數(shù):
Remove-Item -Path C:\temp\DeleteMe -Recurse
當(dāng)前標(biāo)題:PowerShell管理文件和文件夾
本文來源:http://www.dlmjj.cn/article/igiige.html