UserProfiles.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
Set-StrictMode -Version Latest

function Get-AllUsersDesktopFolderPath
{
    <#
    .SYNOPSIS
        Because sometimes the all users desktop folder path can be different this function is a placeholder to find
        the all users desktop folder path. It uses a shell object to find this path.
    #>

    [OutputType([bool])]
    [CmdletBinding()]
    param ()
    process
    {
        try
        {
            $Shell = New-Object -ComObject "WScript.Shell"
            $Shell.SpecialFolders.Item('AllUsersDesktop')
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-AllUsersProfileFolderPath
{
    <#
    .SYNOPSIS
        Because sometimes the all users profile folder path can be different this function is a placeholder to find
        the all users profile folder path ie. C:\ProgramData or C:\Users\All Users. It uses an environment variable
        to find this path.
    #>

    [OutputType([string])]
    [CmdletBinding()]
    param ()
    process
    {
        try
        {
            $env:ALLUSERSPROFILE    
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-AllUsersStartMenuFolderPath
{
    <#
    .SYNOPSIS
        Because sometimes the all users profile folder path can be different this function is a placeholder to find
        the start menu in the all users profile folder path ie. C:\ProgramData or C:\Users\All Users.
    #>

    [OutputType([string])]
    [CmdletBinding()]
    param ()
    process
    {
        try
        {
            
            if (((Get-OperatingSystem) -match 'XP') -or ((Get-OperatingSystem) -match '2003'))
            {
                "$(Get-AllUsersProfileFolderPath)\Start Menu"
            }
            else
            {
                "$(Get-AllUsersProfileFolderPath)\Microsoft\Windows\Start Menu"
            }
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-UserProfile
{
    <#
    .SYNOPSIS
        This function queries the registry to find all of the user profiles
    #>

    [OutputType([System.Management.Automation.PSCustomObject])]
    [CmdletBinding()]
    param ()
    process
    {
        try
        {
            
            Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\*' |
            Select-Object -ExcludeProperty SID *, @{ n = 'SID'; e = { $_.PSChildName } }, @{ n = 'Username'; e = { $_.ProfileImagePath | Split-Path -Leaf } }
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-RootUserProfileFolderPath
{
    <#
    .SYNOPSIS
        Because sometimes the root user profile folder path can be different this function is a placeholder to find
        the root user profile folder path ie. C:\Users or C:\Documents and Settings for any OS. It queries a registry value
        to find this path.
    #>

    [OutputType([string])]
    [CmdletBinding()]
    param ()
    process
    {
        try
        {
            (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' -Name ProfilesDirectory).ProfilesDirectory    
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-UserProfilePath
{
    <#
    .SYNOPSIS
        This function find the folder path of a user profile based off of a number of different criteria. If no criteria is
        used, it will return all user profile paths.
    .EXAMPLE
        PS> .\Get-UserProfilePath -Sid 'S-1-5-21-350904792-1544561288-1862953342-32237'
     
        This example finds the user profile path based on the user's SID
    .EXAMPLE
        PS> .\Get-UserProfilePath -Username 'bob'
     
        This example finds the user profile path based on the username
    .PARAMETER Sid
         The user SID
    .PARAMETER Username
        The username
    #>

    [OutputType([string])]
    [CmdletBinding(DefaultParameterSetName = 'None')]
    param (
        [Parameter(ParameterSetName = 'SID')]
        [string]$Sid,
        
        [Parameter(ParameterSetName = 'Username')]
        [string]$Username
    )
    
    process
    {
        try
        {
            if ($Sid)
            {
                $WhereBlock = { $_.PSChildName -eq $Sid }
            }
            elseif ($Username)
            {
                $WhereBlock = { $_.GetValue('ProfileImagePath').Split('\')[-1] -eq $Username }
            }
            else
            {
                $WhereBlock = { $null -ne $_.PSChildName }
            }
            Get-ChildItem 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList' | Where-Object $WhereBlock | ForEach-Object { $_.GetValue('ProfileImagePath') }
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Remove-ProfileItem
{
    <#
    .SYNOPSIS
        This function removes a file(s) or folder(s) with the same path in all user profiles including
        system profiles like SYSTEM, NetworkService and AllUsers.
    .EXAMPLE
        PS> .\Remove-ProfileItem -Path 'AppData\Adobe'
     
        This example will remove the folder path 'AppData\Adobe' from all user profiles
    .PARAMETER Path
        The path(s) to the file or folder you'd like to remove.
    #>

    [OutputType()]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$Path
    )
    
    process
    {
        try
        {
            
            $AllUserProfileFolderPath = Get-AllUsersProfileFolderPath
            $UserProfileFolderPaths = Get-UserProfilePath
            
            foreach ($p in $Path)
            {
                if (-not (Test-Path "$AllUserProfileFolderPath\$p"))
                {
                    Write-Log -Message "The folder '$AllUserProfileFolderPath\$p' does not exist"
                }
                else
                {
                    Remove-Item -Path "$AllUserProfileFolderPath\$p" -Force -Recurse
                }
                
                
                foreach ($ProfilePath in $UserProfileFolderPaths)
                {
                    if (-not (Test-Path "$ProfilePath\$p"))
                    {
                        Write-Log -Message "The folder '$ProfilePath\$p' does not exist"
                    }
                    else
                    {
                        Remove-Item -Path "$ProfilePath\$p" -Force -Recurse
                    }
                }
            }
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Set-AllUserStartupAction
{
    <#
    .SYNOPSIS
        A function that executes a command line for the any current logged on user and uses the Active Setup registry key to set a
        registry value that contains a command line EXE with arguments that will be executed once for every user that logs in.
    .PARAMETER CommandLine
        The command line string that will be executed once at every user logon
    #>

    [OutputType()]
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory = $true)]
        [string]$CommandLine
    )
    process
    {
        try
        {
            if ($PSCmdlet.ShouldProcess($CommandLine,'set all startup action')) {
                ## Create the Active Setup registry key so that the reg add cmd will get ran for each user
                ## logging into the machine.
                ## http://www.itninja.com/blog/view/an-active-setup-primer
                $Guid = [guid]::NewGuid().Guid
                Write-Log -Message "Created GUID '$Guid' to use for Active Setup"
                $ActiveSetupRegParentPath = 'HKLM:\Software\Microsoft\Active Setup\Installed Components'
                New-Item -Path $ActiveSetupRegParentPath -Name $Guid -Force | Out-Null
                $ActiveSetupRegPath = "HKLM:\Software\Microsoft\Active Setup\Installed Components\$Guid"
                Write-Log -Message "Using registry path '$ActiveSetupRegPath'"
                Write-Log -Message "Setting command line registry value to '$CommandLine'"
                Set-ItemProperty -Path $ActiveSetupRegPath -Name '(Default)' -Value 'Active Setup Test' -Force
                Set-ItemProperty -Path $ActiveSetupRegPath -Name 'Version' -Value '1' -Force
                Set-ItemProperty -Path $ActiveSetupRegPath -Name 'StubPath' -Value $CommandLine -Force
            }
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}