簡體   English   中英

編寫 PowerShell 腳本以將目錄及其內容復制到所有用戶的文檔文件夾

[英]Writing a PowerShell script to copy directory and its contents to all users documents folder

首先,這是我關於堆棧溢出的第一個真正的問題,所以我們在這里 go:

我正在嘗試使用 Get-ChildItem 和 Copy-Item 將目錄及其內容復制到可能位於 PC 上的每個用戶配置文件,而不知道有多少用戶或他們的用戶名。 但是,每當我運行腳本或我認為實現結果的方式相同的其他變體時,我都會遇到非法字符問題。 我認為這是因為通配符,但我不知道我應該如何規避它。

例如:測試通配符:

Get-ChildItem 'C:Users\*\'

我可以看到所有用戶和來自 Microsoft 的 Power-shell 文檔使用 * 應該允許它包含該文件夾中的所有項目。 但是每當我將路徑擴展到讓我們說:

Copy-Item 'D:\Custom Office Templates' -Recurse -Destination 'C:\Users\*\Documents\' -Force

我收到“路徑錯誤中的非法字符”。 如果我用實際用戶名替換 * 以更正路徑,我會得到我想要的單個用戶。 嘗試使用時會發生同樣的事情。

ForEach-Object {Copy-Item -Path 'D:\Custom Office Templates' -Destination 'C:\users\*\Documents\Custom Office Templates' -Force -Recurse}

感謝您提供的任何幫助:)。

-Destination被讀取為文字路徑,不理解通配符。 您可以做的是將您已經擁有的內容與Get-ChildItem結合使用-Path上的通配符和-Destination上的延遲綁定腳本塊

$source = 'D:\Custom Office Templates'
Get-ChildItem 'C:Users\*\' -Directory |
    Copy-Item -LiteralPath $source -Recurse -Destination {
        Join-Path $_.FullName 'Documents\Custom Office Templates'
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM