Reputation: 1904
I would like to export everything after share path like below. Is there anyway to do this using Split-Path
? Example:
What I have returned in the File is: C:\Data2\Documents\TEST2\TEST2_1\TEST2_2
What I would like to see after the split-path is: TEST2\TEST2_1\TEST2_2
Code:
$Shares = Get-WmiObject Win32_Share -Filter "not name like '%$'" |
Select path
$results = @()
foreach ($Share in $Shares) {
$results += Get-ChildItem -Path $Share.Path -Recurse |
Select-Object -ExpandProperty FullName
}
$results
$results | Out-File -FilePath "C:\Output\list.txt"
Get-WmiObject Win32_Share -Filter "not name like '%$'" | Select path
Output:
path ---- C:\Data2\Documents C:\IT_Information
list.txt
output:
C:\Data2\Documents\TEST C:\Data2\Documents\TEST2 C:\Data2\Documents\TEST3 C:\Data2\Documents\1.txt C:\Data2\Documents\2.txt C:\Data2\Documents\3.txt C:\Data2\Documents\4.txt C:\Data2\Documents\TEST\5.txt C:\Data2\Documents\TEST\6.txt C:\Data2\Documents\TEST\7.txt C:\Data2\Documents\TEST2\TEST2_1 C:\Data2\Documents\TEST2\TEST2_1\TEST2_2 C:\IT_Information\TEST C:\IT_Information\TEST2 C:\IT_Information\TEST3 C:\IT_Information\1.txt C:\IT_Information\2.txt C:\IT_Information\3.txt C:\IT_Information\4.txt C:\IT_Information\TEST\5.txt C:\IT_Information\TEST\6.txt C:\IT_Information\TEST\7.txt C:\IT_Information\TEST2\TEST2_1 C:\IT_Information\TEST2\TEST2_1\TEST2_2
Desired output:
TEST TEST2 TEST3 1.txt 2.txt 3.txt 4.txt TEST\5.txt TEST\6.txt TEST\7.txt TEST2\TEST2_1 TEST2\TEST2_1\TEST2_2 TEST TEST2 TEST3 1.txt 2.txt 3.txt 4.txt TEST\5.txt TEST\6.txt TEST\7.txt TEST2\TEST2_1 TEST2\TEST2_1\TEST2_2
Upvotes: 0
Views: 180
Reputation: 200413
Build a regular expression from the share paths and replace them when they occur at the beginning of a path:
$Shares = Get-WmiObject Win32_Share -Filter "not name like '%$'" |
Select-Object -Expand Path
$re = ($Shares | ForEach-Object {[Regex]::Escape($_)}) -join '|'
$results = foreach ($Share in $Shares) {
(Get-ChildItem $Share -Recurse | Select-Object -Expand FullName) -replace "^($re)\\"
}
Upvotes: 1