forked from supersysadmin/WindowsPowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find-FSDirectory.ps1: Initial version.
- Loading branch information
1 parent
249032f
commit 2ec3e0b
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<# | ||
.SYNOPSIS | ||
Function to find directories which match a certain name. | ||
.DESCRIPTION | ||
Function to find directories which match a certain name. | ||
.PARAMETER Path | ||
In the parameter Path you can specify the directory you want to query. This can be both a local or remote (UNC) path. | ||
.PARAMETER SearchString | ||
In the parameter SearchString you can specify the (partial) directory you want to find. | ||
.PARAMETER Recurse | ||
When using the switch Recurse the function will also search in subdirectories. | ||
.EXAMPLE | ||
PS C:\>Find-FSDirectory -Path C:\Windows -SearchString "System" -Recurse | ||
This command will retrieve a list of directories in C:\Windows (and subdirectories) which have "System" in their name. | ||
.NOTES | ||
Author : Ingvald Belmans | ||
Website : http://www.supersysadmin.com | ||
Version : 1.0 | ||
Changelog: | ||
- 1.0 (2015-12-24) Initial version. | ||
.LINK | ||
http://www.supersysadmin.com | ||
#> | ||
function Find-FSDirectory | ||
{ | ||
[CmdletBinding()] | ||
Param | ||
( | ||
[Parameter( | ||
Mandatory=$true, | ||
ValueFromPipeline=$true, | ||
ValueFromPipelineByPropertyName=$true | ||
) | ||
] | ||
[String] | ||
$Path, | ||
[Parameter( | ||
Mandatory=$true, | ||
ValueFromPipeline=$true, | ||
ValueFromPipelineByPropertyName=$true | ||
) | ||
] | ||
[String] | ||
$SearchString, | ||
[switch] | ||
$Recurse | ||
) | ||
Begin | ||
{ | ||
} | ||
Process | ||
{ | ||
Write-Verbose -Message "Searching for directories in '$Path' which match '$SearchString'. Please wait..." | ||
$SearchResult = @() | ||
if($Recurse) | ||
{ | ||
$SearchQuery = Get-ChildItem -Path $Path -Directory -Recurse | Where-Object -FilterScript { $_.Name -match $SearchString} | ||
} | ||
else | ||
{ | ||
$SearchQuery = Get-ChildItem -Path $Path -Directory | Where-Object -FilterScript { $_.Name -match $SearchString} | ||
} | ||
foreach ($SearchQueryItem in $SearchQuery) | ||
{ | ||
$SearchResultObject = New-Object -TypeName System.Object | ||
$SearchResultObject | Add-Member -MemberType NoteProperty -Name "DirectoryName" -Value $SearchQueryItem.Name | ||
$SearchResultObject | Add-Member -MemberType NoteProperty -Name "FullPath" -Value $SearchQueryItem.FullName | ||
$SearchResultObject | Add-Member -MemberType NoteProperty -Name "LastWriteTime" -Value $SearchQueryItem.LastWriteTime | ||
$SearchResultObject | Add-Member -MemberType NoteProperty -Name "LastAccessTime" -Value $SearchQueryItem.LastAccessTime | ||
$SearchResult += $SearchResultObject | ||
} | ||
Write-Output -InputObject $SearchResult | ||
} | ||
End | ||
{ | ||
} | ||
} |