-
Notifications
You must be signed in to change notification settings - Fork 14
/
Find-File.ps1
83 lines (60 loc) · 2.79 KB
/
Find-File.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<#
.SYNOPSIS
Find-File is a cmdlet created to help a user find a file they only remeber part of the name of.
It can also be used to find the location of a file where the name is remember but the location is not.
This cmdlet was designed for users. As such no switches need to be defined. Running the cmdlet will prompt the user for input.
.DESCRIPTION
This cmdlet searches the C: Drive for a rough file name and returns its location.
If more than one file are found, more than one location will be returned.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.EXAMPLE
Find-File
#>
Function Find-File {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="The File's Name. `n Use * anywhere you are unsure of what comes next.`n * represents anything. `n Example: *USA*.txt `n If you see this message, you will need to press enter after being prompted to define a second file name. `n This will begin your search. `n Adding another file to search for will not work.")] # End Parameter
[string[]]$FileName) # End param
BEGIN {
Write-Verbose "Begining Search. Please Wait..."
$PathResults = Get-ChildItem -Path 'C:\' -Filter "$FileName" -Recurse -ErrorAction SilentlyContinue -Force
} # End BEGIN
PROCESS {
If ($PathResults) {
ForEach ($Result in $PathResults) {
$Properties = @{
File = $Result
Directory = $Result.DirectoryName
FullPath = $Result.FullName
LastAccessed = $Result.LastAccessTime
LastEdited = $Result.LastWriteTime
Created = $Result.CreationTime
} # End Properties
$Obj = New-Object -TypeName PSCustomObject -Property $Properties
Write-Output $Obj
} # End ForEach
} # End if
Else {
Write-Warning "No file found by that name on the C: Drive. `n If you feel you received this warning in error, `n 1.) Ensure you added a file extension `n 2.) Try to be less specific by using *. `n 3.) Only add one file name to search for"
} # End Else
} # End PROCESS
END {
Write-Verbose "Search Completed"
} # End END
} # End Function