PathFinder.psm1

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
function Get-Path {
    <#
    .SYNOPSIS
        The function converts string with delimiter (e.g. environment variable $env:Path) to array of strings.
    .DESCRIPTION
        The function converts string with delimiter (e.g. environment variable $env:Path) to array of strings.
    .EXAMPLE
        PS C:\> Get-Path -Path $env:Path
        Converts environment variable $env:Path to array of strings. Flag "OnlyAvailable" is set by default, so the function will return only existing paths.
    .EXAMPLE
        PS C:\> Get-Path -Path $env:Path -OnlyAvailable:$false
        Converts environment variable $env:Path to array of strings. Flag "OnlyAvailable" is unset, so the function will return all paths.
    .INPUTS
        [System.String]
    .OUTPUTS
        [System.Array]
    .NOTES
        ...
    #>

    [CmdletBinding()]
    param (
        # Source path from $env:Path, $env:PSModulePath or from elsewhere like $env:SMS_ADMIN_UI_PATH. Default is $env:Path.
        [Parameter(HelpMessage="Source path from env:Path, env:PSModulePath or from elsewhere like env:SMS_ADMIN_UI_PATH. Default is env:Path.")]
        [string]
        $Path = $env:Path,

        # Returns only available paths if checked. Default value is "True".
        [Parameter(HelpMessage="Returns only available paths if checked. Default value is True.")]
        [bool]
        $OnlyAvailable = $true
    )

    begin {
        [array]$pathToArray = $Path.Split(";").Where({$_.Length -ne 0})
        [array]$outArray = @()
    }

    process {
        if ($OnlyAvailable) {
            [array]$tempArray = @()
            foreach ($pathStr in $pathToArray) {
                if (Test-Path -Path $pathStr) {
                    $tempArray += $pathStr
                }
            }
            $outArray = $tempArray
        } else {
            $outArray = $pathToArray
        }
    }

    end {
        return $outArray
    }
}

function Find-Path {
    <#
    .SYNOPSIS
        The function finds path by keyword.
    .DESCRIPTION
        The function finds path by keyword.
    .EXAMPLE
        PS C:\> Find-Path -Keyword putty -Path $env:Path
        Returns from environment variable $env:Path path with keyword "putty":
        PS C:\> C:\Program Files\PuTTY\
    .EXAMPLE
        PS C:\> Find-Path -Keyword pageant -Path $env:Path -InContent
        Function will get paths from $env:Path and will search for files with keyword "pageant", then will return paths to folders where the files are present:
        PS C:\> C:\Program Files\PuTTY\
        PS C:\> C:\Program Files\Git\cmd
    .INPUTS
        [System.String]
    .OUTPUTS
        [System.Array]
    .NOTES
        ...
    #>

    [CmdletBinding()]
    param (
        # Search string
        [Parameter(Mandatory=$true,HelpMessage="Keyword for search, mandatory parameter")]
        [string]
        $Keyword,

        # Path to search, default is $env:Path
        [Parameter(HelpMessage="Path to search, default is env:Path")]
        [string]
        $Path = $env:Path,

        # If true, will search for matches in filenames in path folders. Default is "False".
        [Parameter(HelpMessage="If true, will search for matches in filenames in path folders. Default is False.")]
        [switch]
        $InContent = $false
    )
    
    begin {
        [array]$tempArray = Get-Path -Path $Path
        [array]$outArray = @()
    }
    
    process {
        foreach ($pathStr in $tempArray) {
            if (!$InContent) {
                if ($pathStr -match $Keyword) {
                    $outArray += $pathStr
                }
            } else {
                if ((Get-ChildItem -Path $pathStr -File).Name -match $Keyword) {
                    $outArray += $pathStr
                }
            }
        }
    }
    
    end {
        return $outArray
    }
}

function Add-Path {
    <#
    .SYNOPSIS
        Adds path to environment. Returns string.
    .DESCRIPTION
        Adds path to environment. Returns string.
    .EXAMPLE
        PS C:\> Add-Path -Path "C:\Path\Does\Not\Exists" -Scope $env:Path
        Adds path "C:\Path\Does\Not\Exists" to environment variable $env:Path, if path "C:\Path\Does\Not\Exists" exists.
    .EXAMPLE
        PS C:\> Add-Path -Path "C:\Path\Does\Not\Exists" -Scope $env:Path -OnlyIfExists:$false
        Adds path "C:\Path\Does\Not\Exists" to environment variable $env:Path, even if path "C:\Path\Does\Not\Exists" does NOT exists.
    .INPUTS
        [System.String]
    .OUTPUTS
        [System.String]
    .NOTES
        ...
    #>

    [CmdletBinding()]
    param (
        # Path to add
        [Parameter(Mandatory=$true,HelpMessage="Path to add")]
        [string]
        $Path,

        # Scope. Default is $env:Path.
        [Parameter(HelpMessage="Scope. Default is env:Path.")]
        [string]
        $Scope = $env.Path,

        # Test path before addition. Default is $true.
        [Parameter(HelpMessage="Test path before addition. Default is True.")]
        [bool]
        $OnlyIfExists = $true
    )
    
    begin {
        [array]$tempArray = Get-Path -Path $Scope
        [string]$outString = $null
    }
    
    process {
        if (!$OnlyIfExists) {
            Write-Warning -Message "Adding path $Path without checking for existence!"
            $tempArray += $Path
        } else {
            if (Test-Path -Path $Path) {
                Write-Verbose -Message "Adding path $Path to selected scope $Scope"
                $tempArray += $Path
            } else {
                Write-Error -Message "Path $Path does not exists! Exiting."
                break
            }
        }
        Write-Verbose -Message $tempArray
        $outString = $tempArray -join ";"
    }
    
    end {
        return $outString
    }
}

function Remove-Path {
    <#
    .SYNOPSIS
        Removes path from environment variable.
    .DESCRIPTION
        Removes path from environment variable.
    .EXAMPLE
        PS C:\> Remove-Path -Path "C:\Path\Does\Not\Exists" -Scope $env:Path
        Removes path "C:\Path\Does\Not\Exists" from environment variable $env:Path and return string.
    .INPUTS
        [System.String]
    .OUTPUTS
        [System.String]
    .NOTES
        ...
    #>

    [CmdletBinding()]
    param (
        # Path to remove
        [Parameter(Mandatory=$true,HelpMessage="Path to remove")]
        [string]
        $Path,

        # Scope. Default is $env:Path
        [Parameter(HelpMessage="Scope. Default is env:Path")]
        [string]
        $Scope = $env:Path
    )
    
    begin {
        [array]$tempArray = Get-Path -Path $Scope
        [array]$outArray = @()
        [string]$outString = $null
    }
    
    process {
        foreach ($pathStr in $tempArray) {
            if (!($pathStr -ne $Path)) {
                $outArray += $pathStr
            }
        }
        $outString = $outArray -join ";"
    }
    
    end {
        return $outString
    }
}

Export-ModuleMember -Function @(
    "Get-Path",
    "Find-Path",
    "Add-Path",
    "Remove-Path"
    )