ProfilePal.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# Requires -Version 3.0
<#
    .SYNOPSIS
        ProfilePal Module contains functions that help create and edit PowerShell profiles, as well as some other functions which can easily be re-used across all PowerShell profiles
    .DESCRIPTION
        ProfilePal Module provides helpful functions for customizing PowerShell profiles, and includes a couple 'bonus' functions for making PowerShell a bit easier to work with. Intended to help new(er) PowerShell users more quickly discover the value of managing and customizing their own PowerShell Profile.
        Functions:
        Get-Profile - Enumerates basic info of common PowerShell Profiles
        New-Profile - Creates PowerShell Profiles, and customizes the console, with tips to get more familiar about managing one's own profile customizations and preferences
        Edit-Profile - Opens a specified PowerShell profile in the PowerShell_ISE, for editing
        Suspend-Profile - Suspends an active PowerShell profile by renaming (appending) the filename. Helpful with testing or troubleshooting changes or potential conflicts between profiles. To reload a PowerShell session without the suspended profile, exit and restart the pertinent PowerShell host.
        Resume-Profile - Resumes an suspended PowerShell profile, to be active in the next PowerShell session, by restoring a profile script file renamed by Suspend-Profile.
        Reset-Profile - Simply reloads the current profile script (`. $Profile`), but 'reload' is not an approved PowerShell verb, so we call it Reset.
 
        Get-UserName - Returns active user's account info in the format of DOMAIN\AccountName
        prompt - Overrides the default prompt, removing the pwd/path element, and conditionally adds an [ADMIN] indicator, in place of the default Administrator string in the window title bar. Customizing prompt is explained in detail in the PowerShell help file about_Prompts (try `get-help about_Prompts`)
        Get-WindowTitle - Stores active $host window title, in support of Set-WindowTitle and Reset-WindowTitle functions
        Set-WindowTitle - Customizes PS $host window title, to show version, starting path, and start date/time
        Reset-WindowTitle - Restores default PowerShell host window title, as captured by Get-WindowTitle
        Start-RemoteDesktop - Launch a Windows Remote Desktop admin session to a specified computername, with either FullScreen, or sized window
        Open-AdminConsole - Launch a new console window from the command line, with elevated (admin) permissions
        Test-Port - Effectively a PowerShell native-alternative / replacement for telnet, for testing IP port(s) of a remote computer
 
    .NOTES
        File Name : ProfilePal.psm1
        Author : Bryan Dady
        Link Note : Some functions originally inspired by zerrouki
        Thanks zerrouki for the inspiration! http://www.zerrouki.com/powershell-profile-example/
    .LINK
        http://bryan.dady.us/profilepal/
        https://github.com/bcdady/profilepal
#>


# Define script scope variables we might need later
[Boolean]$FrameTitleDefault
[String]$defaultFrameTitle

function Get-WindowTitle 
{
<#
    .SYNOPSIS
        Stores the default PowerShell host window title
    .DESCRIPTION
        Supports Set-WindowTitle and Reset-WindowTitle functions
#>

    if ($FrameTitleDefault) 
    {
        $defaultFrameTitle = $Host.UI.RawUI.WindowTitle 
    }
    $FrameTitleDefault = $true
}

function Set-WindowTitle 
{
<#
    .SYNOPSIS
        Customizes Host window title, to show version, starting path, and start date/time. With the path in the title, we can leave it out of the prompt, to simplify and save console space.
    .DESCRIPTION
        For use in customizing PowerShell Host look and feel, in conjunction with a customized prompt function
       Customizes Host window title, to show version, starting path, and start date/time (in "UniversalSortableDateTimePattern using the format for universal time display" - per https://technet.microsoft.com/en-us/library/ee692801.aspx)
#>

    Get-WindowTitle
    $hosttime = Get-Date (Get-Process -Id $PID).StartTime -Format u
    [String]$hostVersion = $Host.version
    [String]$titlePWD    = Get-Location
    $Host.UI.RawUI.WindowTitle = "PowerShell $hostVersion - $titlePWD [$hosttime]"
    $FrameTitleDefault = $false
}

New-Alias -Name Update-WindowTitle -Value Set-WindowTitle -ErrorAction Ignore
function Reset-WindowTitle 
{
<#
    .SYNOPSIS
        Restores default PowerShell host window title, as captured by Get-WindowTitle
    .DESCRIPTION
        Provided to make it easy to reset the default window frame title, but presumes that Get-WindowTitle was previously run
#>

    Write-Debug -InputObject $defaultFrameTitle 
    Write-Debug -InputObject "FrameTitle length: $($defaultFrameTitle.length)"
    if ($defaultFrameTitle.length -gt 1) 
    {
        $Host.UI.RawUI.WindowTitle = $defaultFrameTitle
    }
}

function prompt 
{
<#
    .SYNOPSIS
        Overrides the default prompt, to remove the pwd/path element from each line, and conditionally adds an indicator of the $host running with elevated permsisions ([ADMIN]).
    .DESCRIPTION
        From about_Prompts: "The Windows PowerShell prompt is determined by the built-in Prompt function. You can customize the prompt by creating your own Prompt function and saving it in your Windows PowerShell profile".
 
        See http://poshcode.org/3997 for more cool prompt customization ideas
 
#>

    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [Security.Principal.WindowsPrincipal] $identity

    $(  if ($PSDebugContext)
            {'[DEBUG] ' }
        elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
            {'[ADMIN] '}
        else 
        {''}
            ) + 'PS .\' + $(if ($nestedpromptlevel -ge 1) 
                { ' >> ' }
    ) + '> '
}

function Open-AdminConsole 
{
<#
    .SYNOPSIS
        Launch a new console window from the command line, with optional -NoProfile support
    .DESCRIPTION
        Simplifies opening a PowerShell console host, with Administrative permissions, by enabling the same result from the keyboard, instead of having to grab the mouse to Right-Click and select 'Run as Administrator'
        The following aliases are also provided:
        Open-AdminHost
        Start-AdminConsole
        Start-AdminHost
        New-AdminConsole
        New-AdminHost
        Request-AdminConsole
        Request-AdminHost
        sudo
#>

    # Aliases added below
    Param( [Switch]$noprofile )

    if ($Variable:noprofile) 
        { Start-Process -FilePath "$PSHOME\powershell.exe" -ArgumentList '-NoProfile' -Verb RunAs -WindowStyle Normal}
    else
        { Start-Process -FilePath "$PSHOME\powershell.exe" -Verb RunAs -WindowStyle Normal
    }
}

New-Alias -Name Open-AdminHost -Value Open-AdminConsole -ErrorAction Ignore

New-Alias -Name Start-AdminConsole -Value Open-AdminConsole -ErrorAction Ignore

New-Alias -Name Start-AdminHost -Value Open-AdminConsole -ErrorAction Ignore

New-Alias -Name New-AdminConsole -Value Open-AdminConsole -ErrorAction Ignore

New-Alias -Name New-AdminHost -Value Open-AdminConsole -ErrorAction Ignore

New-Alias -Name sudo -Value Open-AdminConsole -ErrorAction Ignore

function Get-Profile 
# Future enhancement: update how we create PSobjects, e.g. w/ templates, per: http://www.powershellmagazine.com/2013/02/04/creating-powershell-custom-objects/
{
<#
    .SYNOPSIS
        Returns corresponding PowerShell profile name, path, and status (whether it's script file exists or not)
    .DESCRIPTION
        Can be passed a parameter for a profile by Name or Path, and returns a summary object
    .PARAMETER Name
        Accepts 'AllProfiles', 'CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost' or 'AllUsersAllHosts'
    .EXAMPLE
        PS .\> Get-Profile
 
        Name Path Exists
        ----------- ----------- --------------
        CurrentUserCurrentHost C:\Users\BDady\Documents\WindowsPowerSh... True
 
    .EXAMPLE
        PS .\> Get-Profile -Name AllUsersCurrentHost | Format-Table -AutoSize
 
        Name Path Exists
        ----------- ----------- --------------
        AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 False
 
    .NOTES
        NAME : Get-Profile
        LAST UPDATED: 4/27/2015
        AUTHOR : Bryan Dady
    .INPUTS
        None
    .OUTPUTS
        Profile Object
#>

    [CmdletBinding()]
    Param (
        # Specifies which profile to check; if not specified, presumes default result from $PROFILE
        [Parameter(Mandatory = $false,
                Position = 0,
                ValueFromPipeline = $false,
                ValueFromPipelineByPropertyName = $false,
        HelpMessage = 'Specify $PROFILE by Name, such as CurrenUserCurrentHost')]
        [ValidateSet('AllProfiles','CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost', 'AllUsersAllHosts')]
        [string]
        $Name = 'AllProfiles'
    )

    # Define empty array to add profile return objects to
    [array]$returnCollection = @()

    # Build a hashtable to easily enumerate PowerShell profile contexts / names and their scripts
    [hashtable]$hashProfiles = @{
        CurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
        CurrentUserAllHosts    = $PROFILE.CurrentUserAllHosts
        AllUsersCurrentHost    = $PROFILE.AllUsersCurrentHost
        AllUsersAllHosts       = $PROFILE.AllUsersAllHosts
    }

    # Check if a $PROFILE script is found on the file system, for the profile specified by the Name parameter, then return details for that profile script
    Switch ($Name) {
        'AllProfiles' 
        {
            $hashProfiles.Keys | ForEach-Object -Process {
                if (Test-Path -Path $hashProfiles.$PSItem -ErrorAction SilentlyContinue)
                    { $ProfileExists = $true }
                else 
                    { $ProfileExists = $false
                }

                $properties = @{
                    'Exists' = $ProfileExists
                    'Name'   = $PSItem
                    'Path'   = $hashProfiles.$PSItem
                }
                $object = New-Object -TypeName PSObject -Property $properties

                # Add this resulting object to the array object to be returned by this function
                $returnCollection += $object

                # cleanup properties variable
                Clear-Variable -Name properties
            }
        }
        Default 
        {
            if (Test-Path -Path $hashProfiles.$Name -ErrorAction SilentlyContinue)
                { $ProfileExists = $true }
            else 
                { $ProfileExists = $false
            }

            #'Optimize New-Object invocation, based on Don Jones' recommendation: https://technet.microsoft.com/en-us/magazine/hh750381.aspx
            $properties = @{
                'Name' = $Name
                'Path' = $hashProfiles.$Name
                'Exists' = $ProfileExists
            }
            $object = New-Object -TypeName PSObject -Property $properties

            # Add this resulting object to the array object to be returned by this function
            $returnCollection = $object
        }
    }

    return $returnCollection | Sort-Object -Property Name
}

function Edit-Profile 
{
<#
    .Synopsis
        Open a PowerShell Profile script in the ISE editor
    .DESCRIPTION
        Edit-Profile will attempt to open any existing PowerShell Profile scripts, and if none are found, will offer to invoke the New-Profile cmdlet to build one
        Both New-Profile and Edit-Profile can open any of the 4 contexts of PowerShell Profile scripts.
    .PARAMETER ProfileName
        Accepts 'CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost' or 'AllUsersAllHosts'
    .EXAMPLE
        Edit-Profile
        Opens the default $profile script file, if it exists
    .EXAMPLE
        Edit-Profile CurrentUserAllHosts
        Opens the specified CurrentUserAllHosts $profile script file, which applies to both Console and ISE hosts, for the current user
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param (
        # Specifies which profile to edit; if not specified, ISE presumes $profile is CurrentUserCurrentHost
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $true,
            Position = 0,
            HelpMessage = 'Specify the PowerShell Profile to modify. <optional>'
        )]
        [ValidateSet('AllUsersAllHosts','AllUsersCurrentHost','CurrentUserAllHosts','CurrentUserCurrentHost')]
        [String]
        $profileName
    )

    [String]$openProfile = ''

    if ($profileName) 
    {
        # check if the profile file exists
        Write-Debug -Message "Testing existence of $profileName profile: $($PROFILE.$profileName)"
        if (Test-Path -Path $PROFILE.$profileName) 
        {
            # file exists, so we can pass it on to be opened
            $openProfile = $PROFILE.$profileName
        }
        else 
        {
            # Specified file doesn't exist. Fortunately we also have a function to help with that
            Write-Output -InputObject "`n$profileName profile not found."
            Write-Output -InputObject 'Preparing to create a starter profile script, using the New-Profile function.'
            New-Profile -ProfileName $profileName
            # Check if the $profile exists, using the get-profile function
            if ((Get-Profile -Name "$profileName").Exists) 
            {
                $openProfile = $PROFILE.$profileName
            }
            else 
            {
                $openProfile = $null
            }
        }

    # otherwise, test for an existing profile, in order of most specific, to most general scope
    }
    elseif (Test-Path -Path $PROFILE.CurrentUserCurrentHost) 
        { $openProfile = $PROFILE.CurrentUserCurrentHost }
    elseif (Test-Path -Path $PROFILE.CurrentUserAllHosts) 
        { $openProfile = $PROFILE.CurrentUserAllHosts }
    elseif (Test-Path -Path $PROFILE.AllUsersCurrentHost) 
        { $openProfile = $PROFILE.AllUsersCurrentHost }
    elseif (Test-Path -Path $PROFILE.AllUsersAllHosts) 
        { $openProfile = $PROFILE.AllUsersAllHosts
    }

    # if a profile is specified, and found, then we open it.
    if ($openProfile) 
        { & powershell_ise.exe -File $openProfile }
    else 
        { Write-Warning -Message 'No existing PowerShell profile was found. Consider running New-Profile to create a ready-to-use profile script.'
    }
}

function New-Profile 
{
<#
    .Synopsis
        Create a new PowerShell profile script
    .DESCRIPTION
        The PowerShell profile script can be created in any 1 of the 4 default contexts, and if not specified, defaults to the most common CurrentUserCurrentHost.
        If this function is called from within PowerShell ISE, the *CurrentHost* profiles will be created with the requisite PowerShellISE_profile prefix
        In order to create new AllUsers profile scripts, this function must be called with elevated (admin) privileges.
    .PARAMETER ProfileName
        Accepts 'CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost' or 'AllUsersAllHosts'
    .EXAMPLE
        PS .\> New-Profile
 
        Creates a new starter profile script for the context Current User / Current [PowerShell] Host
 
        Starter profile CurrentUserCurrentHost has been created. To review and/or modify (in the PowerShell ISE), try the Edit-Profile function.
        For example, run: Edit-Profile -profileName CurrentUserCurrentHost
 
        Directory: C:\Users\[username]\Documents\WindowsPowerShell
 
 
        Mode LastWriteTime Length Name
        ---- ------------- ------ ----
        -a--- 4/27/2015 10:54 AM 2381 Microsoft.PowerShell_profile.ps1
 
    .EXAMPLE
        PS .\> New-Profile -profileName CurrentUserAllHosts
 
        Creates a new starter profile script for the context Current User / Current [PowerShell] Host
 
        Starter profile CurrentUserAllHosts has been created. To review and/or modify (in the PowerShell ISE), try the Edit-Profile function.
        For example, run: Edit-Profile -profileName CurrentUserAllHosts
 
        Directory: C:\Users\[username]\Documents\WindowsPowerShell
 
        Mode LastWriteTime Length Name
        ---- ------------- ------ ----
        -a--- 4/27/2015 10:57 AM 2378 profile.ps1
 
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param (
        # Specifies which profile to edit; if not specified, ISE presumes $profile is CurrentUserCurrentHost
        [Parameter(Mandatory = $false,
                ValueFromPipelineByPropertyName = $true,
        Position = 0)]
        [ValidateSet('AllUsersAllHosts','AllUsersCurrentHost','CurrentUserAllHosts','CurrentUserCurrentHost')]
        [String]
        $profileName = 'CurrentUserCurrentHost'
    )

    # Pre-define new profile script content, which will use functions of this module
    $profile_string_content = @"
# PowerShell `$Profile
# Created by New-Profile function of ProfilePal module
 
# capture starting path so we can go back after other things below might move around
`$startingPath = `$pwd
 
# -Optional- Specify custom font colors
# Uncomment the following if block to tweak the colors of your console; the 'if' statement is to make sure we leave the ISE host alone
# To Uncomment the following block, delete the `<#` from the next line as well as the matching `#`> a few lines down
<#
if (`$host.Name -eq 'ConsoleHost') {
    `$host.ui.rawui.backgroundcolor = 'gray'
    `$host.ui.rawui.foregroundcolor = 'darkblue'
    # clear-host refreshes the background of the console host to the new color scheme
    Clear-Host
    # Wait a second for the clear command to refresh
    Start-Sleep -Seconds 1
    # Write to consolehost a copy of the 'Logo' text displayed when one starts a typical powershell.exe session.
    # This is added in because we'd otherwise not see it, after customizing console colors, and then calling clear-host to refresh the console view
    Write-Output @'
Windows PowerShell [Customized by ProfilePal]
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
'@
 
}
#>
 
Write-Output "``n``tLoading PowerShell ```$Profile`: $profileName``n"
 
# Load profile functions module; includes a customized prompt function
# In case you'd like to edit it, open ProfilePal.psm1 in ISE, and review the function prompt {}
# for more info on prompt customization, you can run get-help about_Prompts
write-output ' # loading ProfilePal Module #'
Import-Module -Name ProfilePal
 
# Do you like easter eggs?: & iex (New-Object Net.WebClient).DownloadString("http://bit.ly/e0Mw9w")
 
# Here's an example of how convenient aliases can be added to your PS profile
New-Alias -Name rdp -Value Start-RemoteDesktop -ErrorAction Ignore
 
# In case any intermediary scripts or module loads change our current directory, restore original path, before it's locked into the window title by Set-WindowTitle
Set-Location `$startingPath
 
# Call Set-WindowTitle function from ProfilePal module
Set-WindowTitle
 
# Display execution policy, for convenience
write-output "``nCurrent PS execution policy is: "
Get-ExecutionPolicy
 
write-output "``n ** To view additional available modules, run: Get-Module -ListAvailable"
write-output "``n ** To view cmdlets available in a given module, run: Get-Command -Module <ModuleName>`n"
 
"@


    Write-Debug -Message $profile_string_content

    # Check if the $profile exists, using the get-profile function
    if ((Get-Profile -Name "$profileName").Exists) 
    {
        Write-Warning -Message "$($PROFILE.$profileName) already exists"
    }
    else 
    {
        # Since a $profile's not created yet, create the file
        # check if we're attempting to create a system context profile
        if ($profileName -like 'AllUsers*') 
        {
            # then we need admin permissions
            if (Test-LocalAdmin) 
            {
                $new_profile = New-Item -type file -Path $PROFILE.$profileName
                # write the profile content into the new file
                Add-Content -Value $profile_string_content -Path $new_profile
            }
            else 
            {
                Write-Warning -Message 'Insufficient privileges to create an AllUsers profile script.'
                Write-Output -InputObject 'Please try again with an Admin console (see function Open-AdminConsole), or create a CurrentUser profile instead.'
            } # end Test-LocalAdmin
        }
        else 
        {
            $new_profile = New-Item -type file -Path $PROFILE.$profileName
            # write the profile content into the new file
            Add-Content -Value $profile_string_content -Path $new_profile
        } # end profileName
    } # end Get-Profile

    # Check / confirm that the $profile exists, using the get-profile function
    if ((Get-Profile -Name "$profileName").Exists) 
    {
        Write-Output -InputObject "`nStarter profile $profileName has been created."
        Write-Output -InputObject ' To review and/or modify (in the PowerShell ISE), try the Edit-Profile function.'
        Write-Output -InputObject " For example, run: Edit-Profile -profileName $profileName"

        return $new_profile
    }
    else 
    {
        return $false
    }
} # end function

New-Alias -Name Initialize-Profile -Value New-Profile -ErrorAction:SilentlyContinue

function Reset-Profile 
{
<#
    .SYNOPSIS
        Reload the profile (`$PROFILE), by using dot-source invocation
    .DESCRIPTION
        Essentially an alias for PS .\>. $Profile
#>

    . $PROFILE
}

function Suspend-Profile 
{
<#
    .SYNOPSIS
        Suspend any active PowerShell profile scripts, by renaming (appending) the filename
        This can be reversed by the corresponding function Resume-Profile
    .DESCRIPTION
        Can be passed a parameter for a profile by Name or Path, and returns a summary object
    .PARAMETER Name
        Accepts 'AllProfiles', 'CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost' or 'AllUsersAllHosts'
    .EXAMPLE
        PS .\> Suspend-Profile
 
        Name Path Exists
        ----------- ----------- --------------
        CurrentUserCurrentHost C:\Users\BDady\Documents\WindowsPowerSh... True
 
    .EXAMPLE
        PS .\> Suspend-Profile -Name AllProfiles | Format-Table -AutoSize
 
        Name Path Exists
        ----------- ----------- --------------
        AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 False
 
    .NOTES
        NAME : Suspend-Profile
        LAST UPDATED: 7/27/2015
        AUTHOR : Bryan Dady
 
#>

    [CmdletBinding()]
    Param (
        # Specifies which profile to check; if not specified, presumes default result from $PROFILE
        [Parameter(Mandatory = $false,
                Position = 0,
                ValueFromPipeline = $false,
                ValueFromPipelineByPropertyName = $false,
        HelpMessage = 'Specify $PROFILE by Name, such as CurrenUserCurrentHost')]
        [ValidateSet('AllProfiles','CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost', 'AllUsersAllHosts')]
        [string]
        $Name = 'CurrentUserCurrentHost'
    )

    # Define empty array to add profile return objects to
    [array]$returnCollection = @()

    # Build a hashtable to easily enumerate PowerShell profile contexts / names and their scripts
    [hashtable]$hashProfiles = @{
        CurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
        CurrentUserAllHosts    = $PROFILE.CurrentUserAllHosts
        AllUsersCurrentHost    = $PROFILE.AllUsersCurrentHost
        AllUsersAllHosts       = $PROFILE.AllUsersAllHosts
    }

    # Check if a $PROFILE script is found on the file system, for the profile specified by the Name parameter, then return details for that profile script
    Switch ($Name) {
        'AllProfiles' 
        {
            $hashProfiles.Keys | ForEach-Object -Process {
                if (Test-Path -Path $hashProfiles.$PSItem -ErrorAction SilentlyContinue)
                {
                    # RFE : support -Confirm parameter
                    $ProfileExists = $true
                    $newPath = Rename-Item -Path $hashProfiles.$PSItem -NewName "$($hashProfiles.$PSItem)~" -Confirm -PassThru
                    Write-Verbose -Message "Assigned `$newPath to $($newPath)"
                }
                else 
                {
                    $ProfileExists = $false
                    $newPath = $null
                    Write-Debug -Message '$ProfileExists = $false; $newPath is $null'
                }

                $properties = @{
                    'Exists' = $ProfileExists
                    'Name'   = $PSItem
                    'Path'   = $newPath.FullName
                }
                $object = New-Object -TypeName PSObject -Property $properties

                # Add this resulting object to the array object to be returned by this function
                $returnCollection += $object

                # cleanup properties variable
                Clear-Variable -Name properties
            }
        }
        Default 
        {
            if (Test-Path -Path $hashProfiles.$Name -ErrorAction SilentlyContinue)
            {
                $ProfileExists = $true
                $newPath = Rename-Item -Path $hashProfiles.$Name -NewName "$($hashProfiles.$Name)~" -Confirm -PassThru
                Write-Verbose -Message "Assigned `$newPath to $($newPath)"
            }
            else 
            {
                $ProfileExists = $false
                $newPath = $null
                Write-Debug -Message '$ProfileExists = $false; $newPath is $null'
            }

            #'Optimize New-Object invocation, based on Don Jones' recommendation: https://technet.microsoft.com/en-us/magazine/hh750381.aspx
            $properties = @{
                'Name' = $Name
                'Path' = $newPath.FullName
                'Exists' = $ProfileExists
            }
            $object = New-Object -TypeName PSObject -Property $properties

            # Add this resulting object to the array object to be returned by this function
            $returnCollection = $object
        }
    }

    return $returnCollection | Sort-Object -Property Name | Format-Table -AutoSize
}

function Resume-Profile 
{
<#
    .SYNOPSIS
        Resumes any previously suspended PowerShell profile scripts, by restoring the expected filename
 
    .DESCRIPTION
        Can be passed a parameter for a profile by Name or Path, and returns a summary object
    .PARAMETER Name
        Accepts 'AllProfiles', 'CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost' or 'AllUsersAllHosts'
    .EXAMPLE
        PS .\> Resume-Profile
 
        Name Path Exists
        ----------- ----------- --------------
        CurrentUserCurrentHost C:\Users\BDady\Documents\WindowsPowerSh... True
 
    .EXAMPLE
        PS .\> Resume-Profile -Name AllProfiles | Format-Table -AutoSize
 
        Name Path Exists
        ----------- ----------- --------------
        AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 False
 
    .NOTES
        NAME : Resume-Profile
        LAST UPDATED: 7/27/2015
        AUTHOR : Bryan Dady
 
#>

    [CmdletBinding()]
    Param (
        # Specifies which profile to check; if not specified, presumes default result from $PROFILE
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $false,
            ValueFromPipelineByPropertyName = $false,
        HelpMessage = 'Specify $PROFILE by Name, such as CurrenUserCurrentHost')]
        [ValidateSet('AllProfiles','CurrentUserCurrentHost', 'CurrentUserAllHosts', 'AllUsersCurrentHost', 'AllUsersAllHosts')]
        [string]
        $Name = 'CurrentUserCurrentHost'
    )

    # Define empty array to add profile return objects to
    [array]$returnCollection = @()

    # Build a hashtable to easily enumerate PowerShell profile contexts / names and their scripts
    [hashtable]$hashProfiles = @{
        CurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
        CurrentUserAllHosts    = $PROFILE.CurrentUserAllHosts
        AllUsersCurrentHost    = $PROFILE.AllUsersCurrentHost
        AllUsersAllHosts       = $PROFILE.AllUsersAllHosts
    }

    # Check if a $PROFILE script is found on the file system, for the profile specified by the Name parameter, then return details for that profile script
    Switch ($Name) {
        'AllProfiles' 
        {
            $hashProfiles.Keys | ForEach-Object -Process {
                if (Test-Path -Path "$($hashProfiles.$PSItem)~" -ErrorAction SilentlyContinue)
                {
                    # RFE : support -Confirm parameter
                    $ProfileExists = $true
                    $newPath = Rename-Item -Path "$($hashProfiles.$PSItem)~" -NewName $hashProfiles.$PSItem -Confirm -PassThru
                    Write-Verbose -Message "Assigned `$newPath to $($newPath)"
                }
                else 
                {
                    $ProfileExists = $false
                    $newPath = $null
                    Write-Debug -Message '$ProfileExists = $false; $newPath is $null'
                }

                $properties = @{
                    'Exists' = $ProfileExists
                    'Name'   = $PSItem
                    'Path'   = $newPath.FullName
                }
                $object = New-Object -TypeName PSObject -Property $properties

                 # Add this resulting object to the array object to be returned by this function
                $returnCollection += $object

                # cleanup properties variable
                Clear-Variable -Name properties
            }
        }
        Default 
        {
            if (Test-Path -Path "$($hashProfiles.$Name)~" -ErrorAction SilentlyContinue)
            {
                $ProfileExists = $true
                $newPath = Rename-Item -Path "$($hashProfiles.$Name)~" -NewName $hashProfiles.$Name -Confirm -PassThru
                Write-Debug -Message "Assigned `$newPath to $($newPath)"
            }
            else 
            {
                $ProfileExists = $false
                $newPath = $null
                Write-Debug -Message '$ProfileExists = $false; $newPath is $null'
            }

            #'Optimize New-Object invocation, based on Don Jones' recommendation: https://technet.microsoft.com/en-us/magazine/hh750381.aspx
            $properties = @{
                'Exists' = $ProfileExists
                'Name' = $Name
                'Path' = $newPath.FullName
            }
            $object = New-Object -TypeName PSObject -Property $properties

            # Add this resulting object to the array object to be returned by this function
            $returnCollection = $object
        }
    }

    return $returnCollection | Sort-Object -Property Name | Format-Table -AutoSize
}

function global:Test-LocalAdmin 
{
<#
    .SYNOPSIS
        Test if you have Admin Permissions; returns simple boolean result
    .DESCRIPTION
        ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
        [Security.Principal.WindowsBuiltInRole] 'Administrator')
#>

    ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] 'Administrator')
}

function Start-RemoteDesktop 
{
<#
    .SYNOPSIS
        Launch a Windows Remote Desktop admin session to a specified computername, with either FullScreen, or sized window
    .DESCRIPTION
        Start-RemoteDesktop calls the mstsc.exe process installed on the local instance of Windows.
        By default, Start-RemoteDesktop specifies the optional arguments of /admin, and /fullscreen.
        Start-RemoteDesktop also provides a -ScreenSize parameter, which supports optional window resolution specifications of 1440 x 1050, 1280 x 1024, and 1024 x 768.
        I first made this because I was tired of my last mstsc session hanging on to my last resolution (which would change between when I was docked at my desk, or working from the smaller laptop screen), so this could always 'force' /fullscreen.
    .PARAMETER ComputerName
        Specifies the DNS name or IP address of the computer / server to connect to.
    .PARAMETER ScreenSize
        Specifies the window resolution. If not specified, defaults to Full Screen.
    .PARAMETER Control
        Optional specifies if the remote session should function in Admin, RestrictedAdmin, or Control mode [default in this function].
    .PARAMETER FullScreen
        Unambiguously specifies that the RDP window open to full screen size.
    .PARAMETER PipelineVariable
        Accepts property ComputerName.
    .EXAMPLE
        PS C:\> Start-RemoteDesktop remotehost
        Invokes mstsc.exe /v:remotehost /control
    .EXAMPLE
        PS C:\> Start-RemoteDesktop -ComputerName <IP Address> -ScreenSize 1280x1024 -Control RestrictedAdmin
        Invokes mstsc.exe /v:<IP Address> /RestrictedAdmin /w:1280 /h:1024
    .NOTES
        NAME : Start-RemoteDesktop
        VERSION : 1.7
        LAST UPDATED: 4/4/2015
        AUTHOR : Bryan Dady; @bcdady; http://bryan.dady.us
    .INPUTS
        ComputerName
    .OUTPUTS
        None
#>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory = $true,
                ValueFromPipelineByPropertyName = $true,
        Position = 0)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ComputerName,

        [Parameter(Mandatory = $false,Position = 1)]
        [ValidateSet('FullAdmin','RestrictedAdmin')]
        [String]
        $Control = 'FullAdmin',

        [Parameter(Mandatory = $false,Position = 2)]
        [Switch]
        $FullScreen,

        [Parameter(Mandatory = $false,Position = 3)]
        [ValidateSet('FullScreen','1440x1050','1280x1024','1024x768')]
        [String]
        $ScreenSize = 'FullScreen'
    )

    Write-Output "$(Get-Date) Starting $($PSCmdlet.MyInvocation.MyCommand.Name)"

    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) 
    {
        Write-Output -InputObject "Confirmed network availability of ComputerName $ComputerName"
    }
    else 
    {
        throw "Unable to confirm network availability of ComputerName $ComputerName [Test-Connection failed]"
    }

    switch ($Control) {
        'FullAdmin'  
        {
            $AdminLevel = '/admin' 
        }
        'RestrictedAdmin'  
        {
            $AdminLevel = '/RestrictedAdmin'
        }
        Default      
        {
            $AdminLevel = '/Control'
        }
    }

    if ($FullScreen) 
    {
        $Resolution = '/fullscreen' 
    }
    else 
    {
        switch ($ScreenSize) {
            'FullScreen' 
            {
                $Resolution = '/fullscreen' 
            }
            '1440x1050'  
            {
                $Resolution = '/w:1440 /h:1050'
            }
            '1280x1024'  
            {
                $Resolution = '/w:1280 /h:1024'
            }
            '1024x768'   
            {
                $Resolution = '/w:1024 /h:768'
            }
            Default      
            {
                $Resolution = '/fullscreen' 
            }
        }
    }

    Write-Debug -Message "Start-Process -FilePath mstsc.exe -ArgumentList ""/v:$ComputerName $AdminLevel $Resolution"""

    Start-Process -FilePath mstsc.exe -ArgumentList "/v:$ComputerName $AdminLevel $Resolution" 

    Write-Output "$(Get-Date) Exiting $($PSCmdlet.MyInvocation.MyCommand.Name)`n"
}

function Test-Port 
{
<#
    .SYNOPSIS
        Test-Port is effectively a PowerShell replacement for telnet, to support testing of a specified IP port of a remote computer
    .DESCRIPTION
        Test-Port enables testing for any answer or open indication from a remote network port.
    .PARAMETER Target
        DNS name or IP address of a remote computer or network device to test response from.
    .PARAMETER Port
        IP port number to test on the TARGET.
    .PARAMETER Timeout
        Time-to-live (TTL) parameter for how long to wait for a response from the TARGET PORT.
    .EXAMPLE
        PS C:\> Test-Port RemoteHost 9997
        Tests if the remote host is open on the default Splunk port.
    .NOTES
        NAME : Test-Port
        VERSION : 1.1.1
        LAST UPDATED: 4/4/2015
        AUTHOR : Bryan Dady
    .INPUTS
        None
    .OUTPUTS
        None
#>

    [cmdletbinding()]
    param(
        [parameter(mandatory = $true,
        position = 0)]
        [String]$Target,

        [parameter(mandatory = $true,
        position = 1)]
        [ValidateRange(1,50000)]
        [int32]$Port = 80,

        [int32]$Timeout = 2000
    )
    Write-Output "$(Get-Date) Starting $($PSCmdlet.MyInvocation.MyCommand.Name)"
    $outputobj = New-Object -TypeName PSobject
    $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostName -Value $Target
    if(Test-Connection -ComputerName $Target -Count 2 -ErrorAction SilentlyContinue) 
    {
        $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value 'ONLINE'
    } else 
    {
        $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value 'OFFLINE'
    } 
    $outputobj | Add-Member -MemberType NoteProperty -Name PortNumber -Value $Port
    $Socket = New-Object -TypeName System.Net.Sockets.TCPClient
    $Connection = $Socket.BeginConnect($Target,$Port,$null,$null)
    $null = $Connection.AsyncWaitHandle.WaitOne($Timeout,$false)
    if($Socket.Connected -eq $true) 
    {
        $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value 'Success'
    } else 
    {
        $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value 'Failed'
    }
    $null = $Socket.Close
    $outputobj |
    Select-Object -Property TargetHostName, TargetHostStatus, PortNumber, Connectionstatus |
    Format-Table -AutoSize
    Write-Output "$(Get-Date) Exiting $($PSCmdlet.MyInvocation.MyCommand.Name)`n"
}

New-Alias -Name telnet -Value Test-Port -ErrorAction Ignore
 
function Get-UserName 
{
<#
    .SYNOPSIS
        Get-UserName returns user's account info in the format of DOMAIN\AccountName
    .DESCRIPTION
        [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    .EXAMPLE
        PS C:\> Get-UserName
        Returns DomainName\UserName
    .EXAMPLE
        PS C:\> whoami
        Linux friendly alias invokes Get-UserName
    .NOTES
        NAME : Get-UserName
        VERSION : 1.1
        LAST UPDATED: 3/4/2015
#>


    [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
}

New-Alias -Name whoami -Value Get-UserName -ErrorAction Ignore

Export-ModuleMember -Function * -Alias *