Symlink.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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
# Create module-wide variables.
$script:ModuleRoot = $PSScriptRoot
$script:ModuleVersion = (Import-PowerShellDataFile -Path "$ModuleRoot\Symlink.psd1").ModuleVersion
$script:DataPath = "$env:APPDATA\Powershell\Symlink\database.xml"

# For the debug output to be displayed, $DebugPreference must be set to 'Continue' within the current session.
Write-Debug "`e[4mMODULE-WIDE VARIABLES`e[0m"
Write-Debug "Module root folder: $ModuleRoot"
Write-Debug "Module version: $ModuleVersion"
Write-Debug "Database file: $DataPath"

# Create the module data-storage folder if it doesn't exist.
if (-not (Test-Path -Path "$env:APPDATA\Powershell\Symlink" -ErrorAction Ignore))
{
    New-Item -ItemType Directory -Path "$env:APPDATA" -Name "Powershell\Symlink" -Force -ErrorAction Stop
    Write-Debug "Created database folder!"
}

# Potentially force this module script to dot-source the files, rather than load them in an alternative method.
$doDotSource = $global:ModuleDebugDotSource
$doDotSource = $true # Needed to make code coverage tests work

function Resolve-Path_i
{
    <#
    .SYNOPSIS
        Resolves a path, gracefully handling a non-existent path.
         
    .DESCRIPTION
        Resolves a path into the full path. If the path is invalid,
        an empty string will be returned instead.
         
    .PARAMETER Path
        The path to resolve.
         
    .EXAMPLE
        PS C:\> Resolve-Path_i -Path "~\Desktop"
         
        Returns 'C:\Users\...\Desktop"
 
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string]
        $Path
    )
    
    # Run the command, silencing errors.
    $resolvedPath = Resolve-Path -Path $Path -ErrorAction Ignore
    
    # If NULL, then just return an empty string.
    if ($null -eq $resolvedPath)
    {
        $resolvedPath = ""
    }
    
    Write-Output $resolvedPath
}
function Import-ModuleFile
{
    <#
    .SYNOPSIS
        Loads files into the module on module import.
        Only used in the project development environment.
        In built module, compiled code is within this module file.
         
    .DESCRIPTION
        This helper function is used during module initialization.
        It should always be dot-sourced itself, in order to properly function.
         
    .PARAMETER Path
        The path to the file to load.
         
    .EXAMPLE
        PS C:\> . Import-ModuleFile -File $function.FullName
         
        Imports the code stored in the file $function according to import policy.
         
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Path
    )
    
    # Get the resolved path to avoid any cross-OS issues.
    $resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($Path).ProviderPath
    
    if ($doDotSource)
    {
        # Load the file through dot-sourcing.
        . $resolvedPath    
        Write-Debug "Dot-sourcing file: $resolvedPath"
    }
    else
    {
        # Load the file through different method (unknown atm?).
        $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($resolvedPath))), $null, $null) 
        Write-Debug "Importing file: $resolvedPath"
    }
}

# ISSUE WITH BUILT MODULE FILE
# ----------------------------
# If this module file contains the compiled code below, as this is a "packaged"
# build, then that code *must* be loaded, and you cannot individually import
# and of the code files, even if they are there.
#
#
# If this module file is built, then it contains the class definitions below,
# and on Import-Module, this file is AST analysed and those class definitions
# are read-in and loaded.
#
# It's only once a command is run that this module file is executed, and if at
# that point this file starts to individually import the project files, it will
# end up re-defining the classes, and apparently that seems to cause issues
# later down the line.
#
#
# Therefore to prevent this issue, if this module file has been built and it
# contains the compile code below, that code will be used, and nothing else.
#
# The build script should also not package the individual files, so that the
# *only* possibility is to load the compiled code below and there is no way
# the individual files can be imported, as they don't exist.


# If this module file contains the compiled code, import that, but if it doesn't, then import the
# individual files instead.
$importIndividualFiles = $false
if ("<was built>" -eq '<was not built>')
{
    $importIndividualFiles = $true
    Write-Debug "Module not built! Importing individual files."
}

Write-Debug "`e[4mIMPORT DECISION`e[0m"
Write-Debug "Dot-sourcing: $doDotSource"
Write-Debug "Importing individual files: $importIndividualFiles"

# If importing code as individual files, perform the importing.
# Otherwise, the compiled code below will be loaded.
if ($importIndividualFiles)
{
    Write-Debug "!IMPORTING INDIVIDUAL FILES!"
    
    # Execute Pre-import actions.
    . Import-ModuleFile -Path "$ModuleRoot\internal\preimport.ps1"
    
    # Import all internal functions.
    foreach ($file in (Get-ChildItem "$ModuleRoot\internal\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {
        . Import-ModuleFile -Path $file.FullName
    }
    
    # Import all public functions.
    foreach ($file in (Get-ChildItem "$ModuleRoot\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {    
        . Import-ModuleFile -Path $file.FullName
    }
    
    # Execute Post-import actions.
    . Import-ModuleFile -Path "$ModuleRoot\internal\postimport.ps1"
    
    # End execution here, do not load compiled code below (if there is any).
    return
}

Write-Debug "!LOADING COMPILED CODE!"

#region Load compiled code
enum SymlinkState
{
    Exists
    NotExists
    NeedsCreation
    NeedsDeletion
    Error
}

class Symlink
{
    [string]$Name
    hidden [string]$_Path
    hidden [string]$_Target
    hidden [scriptblock]$_Condition
        
    # Constructor with no creation condition.
    Symlink([string]$name, [string]$path, [string]$target)
    {
        $this.Name = $name
        $this._Path = $path
        $this._Target = $target
        $this._Condition = $null
    }
    
    # Constructor with a creation condition.
    Symlink([string]$name, [string]$path, [string]$target, [scriptblock]$condition)
    {
        $this.Name = $name
        $this._Path = $path
        $this._Target = $target
        $this._Condition = $condition
    }
    
    [string] ShortPath()
    {
        # Return the path after replacing common variable string.
        $path = $this._Path.Replace("$env:APPDATA\", "%APPDATA%\")
        $path = $path.Replace("$env:LOCALAPPDATA\", "%LOCALAPPDATA%\")
        $path = $path.Replace("$env:USERPROFILE\", "~\")
        return $path
    }
    
    [string] FullPath()
    {
        # Return the path after expanding any environment variables encoded as %VAR%.
        return [System.Environment]::ExpandEnvironmentVariables($this._Path)
    }
    
    [string] ShortTarget()
    {
        # Return the path after replacing common variable string.
        $path = $this._Target.Replace($env:APPDATA, "%APPDATA%")
        $path = $path.Replace($env:LOCALAPPDATA, "%LOCALAPPDATA%")
        $path = $path.Replace($env:USERPROFILE, "~")
        return $path
    }
    
    [string] FullTarget()
    {
        # Return the target after expanding any environment variables encoded as %VAR%.
        return [System.Environment]::ExpandEnvironmentVariables($this._Target)
    }
    
    [bool] IsValidPathDirectory()
    {
        # Remove the leaf of the path, as that part is the name the symbolic-link should take,
        # and the link may not be created. This does not invalidate the parent path however, as the parent path
        # must be valid for the link to exist in the first place.
        $parentPath = Split-Path -Path $this.FullPath() -Parent
        
        # Now test that this path to the parent is valid. If this path is valid, then the symbolic link
        # item can be successfully created.
        return Test-Path -Path $parentPath
    }
    
    [bool] IsValidTarget()
    {
        # Test that the target is valid. If any of the parent folders do not exist, or if any environmental
        # variables are used which do not exist, this will return false.
        return Test-Path -Path $this.FullTarget()
    }
    
    # TODO: Deprecate.
    [string] TargetState()
    {
        # Check if the target is a valid path.
        if (Test-Path -Path $this.FullTarget() -ErrorAction Ignore)
        {
            return "Valid"
        }
        else
        {
            # Check if the target has unexpanded environment variables,
            # i.e. variable not present on system, hence path cannot
            # be verified.
            if ($this.FullTarget().Contains("%"))
            {
                return "MissingVariable"
            }
            else
            {
                return "Invalid"
            }
        }
    }
    
    # TODO: Refactor.
    [bool] Exists()
    {
        # Check if the item even exists.
        if ($null -eq (Get-Item -Path $this.FullPath() -ErrorAction Ignore))
        {
            return $false
        }
        # Checks if the symlink item exists and has the correct target.
        if ((Get-Item -Path $this.FullPath() -ErrorAction Ignore).Target -eq $this.FullTarget())
        {
            return $true
        }
        else
        {
            return $false
        }
    }
    
    [string] GetSourceState()
    {
        if (-not $this.IsValidPathDirectory())
        {
            # Part of the path for where the symbolic-link exists cannot be resolved correctly, either because of
            # missing folders or because of a use of an environment variable not present on the system.
            # Since the path cannot be validated, its unknown if the symbolic link item exists correctly or not.
            return "CannotValidate"
        }
        
        if (-not (Get-Item -Path $this.FullPath() -ErrorAction Ignore))
        {
            # The parent part of the path is valid, but the actual symbolic-link item does not exist.
            return "Nonexistent"
        }
        
        if (-not $this.IsValidTarget())
        {
            # The target is invalid, so the symbolic-link item exists but it's unknown if the target it points to
            # doesn't exist or if the target it points to cannot be resolved.
            return "UnknownTarget"
        }
        
        if ((Get-Item -Path $this.FullPath()).Target -eq $this.FullTarget())
        {
            # The target of the symbolic-link matches the stored target.
            return "Existent"
        }
        else
        {
            # The target of the symbolic-link does not match the stored target (may have changed).
            return "IncorrectTarget"
        }
        
        return "Unknown"
    }
    
    [string] GetTargetState()
    {
        if (-not $this.IsValidTarget())
        {
            # The target path cannot be resolved, because either the folders don't properly exist, or because the
            # system lacks an environmental variable for resolving.
            return "Invalid"
        }
        
        return "Valid"
    }
    
    [bool] ShouldExist()
    {
        # If the condition is null, i.e. no condition,
        # assume true by default.
        if ($null -eq $this._Condition) { return $true }
        
        # An if check is here just in case the creation condition doesn't
        # return a boolean, which could cause issues down the line.
        # This is done because the scriptblock can't be validated whether
        # it always returns true/false, since it is not a "proper" method with
        # typed returns.
        if (Invoke-Command -ScriptBlock $this._Condition)
        {
            return $true
        }
        return $false
    }
    
    # TODO: Deprecate.
    [SymlinkState] GetState()
    {
        # Return the appropiate state depending on whether the symlink
        # exists and whether it should exist.
        if ($this.Exists() -and $this.ShouldExist())
        {
            return [SymlinkState]::Exists
        }
        elseif ($this.Exists() -and -not $this.ShouldExist()) 
        {
            return [SymlinkState]::NeedsDeletion
        }
        elseif (-not $this.Exists() -and $this.ShouldExist())
        {
            return [SymlinkState]::NeedsCreation
        }
        elseif (-not $this.Exists() -and -not $this.ShouldExist())
        {
            return [SymlinkState]::NotExists
        }
        return [SymlinkState]::Error
    }
}

<#
.SYNOPSIS
    Reads all of the defined symlink objects.
     
.DESCRIPTION
    Reads all of the defined symlink objects.
     
.EXAMPLE
    PS C:\> $list = Read-Symlinks
     
    Reads all of the symlink objects into a variable, for later manipulation.
     
.INPUTS
    None
     
.OUTPUTS
    System.Collections.Generic.List[Symlink]
     
.NOTES
     
#>

function Read-Symlinks
{
    # Create an empty list.
    $linkList = New-Object -TypeName System.Collections.Generic.List[Symlink]
    
    # If the file doesn't exist, skip any importing.
    if (Test-Path -Path $script:DataPath -ErrorAction SilentlyContinue)
    {
        # Read the xml data in.
        $xmlData = Import-Clixml -Path $script:DataPath
        
        # Iterate through all the objects.
        foreach ($item in $xmlData)
        {
            # Rather than extracting the deserialised objects, which would create a mess of serialised and
            # non-serialised objects, create new identical copies from scratch.
            if ($item.pstypenames[0] -eq "Deserialized.Symlink")
            {
                # Create using the appropiate constructor.
                $link = if ($null -eq $item._Condition)
                {
                    [Symlink]::new($item.Name, $item._Path, $item._Target)
                }else
                {
                    [Symlink]::new($item.Name, $item._Path, $item._Target, [scriptblock]::Create($item._Condition))
                }
                
                $linkList.Add($link)
            }
        }
    }
    
    # Return the list as a <List> object, rather than as an array, (ps converts by default).
    Write-Output $linkList -NoEnumerate
}


<#
.SYNOPSIS
    Creates the symbolic-link items.
     
.DESCRIPTION
    The `Build-Symlink` cmdlet creates the symbolic-link items on the
    filesystem. Non-existent items will be created anew, whilst existing items
    will be updated (if necessary). This cmdlet does not create any new
    symlink definitions.
     
.PARAMETER Names
    Specifies the name(s) of the symlinks to create.
     
 [!]This parameter will autocomplete to valid symlink names.
     
.PARAMETER All
    Specifies to create all symlinks.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.PARAMETER Force
    Forces this cmdlet to create a symbolic-link item on the filesystem even
    if the creation condition evaluates to false.
     
    Even using this parameter, if the filesystem denies access to the necessary
    files, this cmdlet can fail.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the
        symlinks to create.
     
.OUTPUTS
    Symlink
     
.NOTES
    This command is aliased by default to 'bsl'.
     
.EXAMPLE
    PS C:\> Build-Symlink -All
     
    Creates all of the symbolic-link items on the filesystem for all symlink
    definitions, assuming the creation condition is met.
     
.EXAMPLE
    PS C:\> Build-Symlink -Names "data","files"
     
    Creates the symbolic-link items on the filesystem for the symlink
    definitions named "data" and "files", assuming any creation conditions for
    each evaluate to true.
     
.LINK
    New-Symlink
    Get-Symlink
    Set-Symlink
    Remove-Symlink
    about_Symlink
     
#>

function Build-Symlink
{
    [Alias("bsl")]
    
    [CmdletBinding(DefaultParameterSetName = "All", SupportsShouldProcess = $true)]
    param
    (
        
        # Tab completion.
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName, ParameterSetName = "Specific")]
        [Alias("Name")]
        [string[]]
        $Names,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "All")]
        [switch]
        $All,
        
        [Parameter()]
        [switch]
        $Force
        
    )

    begin
    {
        # Validate that '-WhatIf'/'-Confirm' isn't used together with '-Force'.
        # This is ambiguous, so warn the user instead.
        Write-Debug "`$WhatIfPreference: $WhatIfPreference"
        Write-Debug "`$ConfirmPreference: $ConfirmPreference"
        if ($WhatIfPreference -and $Force)
        {
            Write-Error "You cannot specify both '-WhatIf' and '-Force' in the invocation for this cmdlet!"
            return
        }
        if (($ConfirmPreference -eq "Low") -and $Force)
        {
            Write-Error "You cannot specify both '-Confirm' and '-Force' in the invocation for this cmdlet!"
            return
        }
    
        # Store lists to notify user which symlinks were created.
        $createdList = New-Object System.Collections.Generic.List[Symlink] 
        
        if ($All)
        {
            $linkList = Read-Symlinks
        }
        else
        {
            $linkList = Get-Symlink -Names $Names -Verbose:$false
        }
    }
    
    process
    {
        foreach ($link in $linkList)
        {
            # Check if the symlink should be created, but it has an invalid target,
            # as in such a case it must be skipped.
            if (($link.ShouldExist() -or $Force) -and ($link.TargetState() -ne "Valid"))
            {
                Write-Error "The symlink named '$($link.Name)' has a target which is invalid/non-existent!`nAborting creation of this symlink."
                continue
            }
            
            # Build the symbolic-link item on the filesytem.
            $expandedPath = $link.FullPath()
            if (($link.ShouldExist() -or $Force) -and ($link.TargetState() -eq "Valid") -and $PSCmdlet.ShouldProcess("Creating symbolic-link item at '$expandedPath'.", "Are you sure you want to create the symbolic-link item at '$expandedPath'?", "Create Symbolic-Link Prompt"))
            {
                # Appropriately delete any existing items before creating the symbolic-link.
                $item = Get-Item -Path $expandedPath -ErrorAction Ignore
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself,
                        # whilst calling 'Delete()' on a folder will not delete it's contents. Therefore check
                        # whether the item is a symbolic-link to call the appropriate method.
                        if ($null -eq $item.LinkType)
                        {
                            Remove-Item -Path $expandedPath -Force -Recurse -ErrorAction Stop -WhatIf:$false `
                                -Confirm:$false | Out-Null
                        }
                        else
                        {
                            $item.Delete()
                        }
                    }
                    catch
                    {
                        Write-Error "The item located at '$expandedPath' could not be deleted to make room for the symbolic-link."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
                
                New-Item -ItemType SymbolicLink -Path $link.FullPath() -Value $link.FullTarget() -Force `
                    -WhatIf:$false -Confirm:$false | Out-Null
                
                $createdList.Add($link)
            }
        }
    }
    
    end
    {
        # By default, outputs in List formatting.
        if ($createdList.Count -gt 0)
        {
            Write-Host "Created the following new symlinks:"
            Write-Output $createdList
        }
    }
}

<#
.SYNOPSIS
    Gets the specified symlink item(s).
     
.DESCRIPTION
    The `Get-Symlink` cmdlet gets one or more symlinks, specified by their
    name(s).
     
.PARAMETER Names
    Specifies the name(s) of the items to get.
     
 [!]This parameter will autocomplete to valid symlink names.
     
.PARAMETER All
    Specifies to get all symlinks.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the
        symlinks to get.
     
.OUTPUTS
    Symlink
     
.NOTES
    This command is aliased by default to 'gsl'.
     
.EXAMPLE
    PS C:\> Get-Symlink -Names "data","files"
     
    Gets the symlink definitions named "data" and "video", and pipes them out
    to the screen, by default formatted in a list.
     
.EXAMPLE
    PS C:\> Get-Symlink -All
     
    Gets all symlink definitions, and pipes them out to the screen, by default
    formatted in a list.
     
.EXAMPLE
    PS C:\> Get-Symlink "data" | Build-Symlink
     
    Gets the symlink definition named "data", and then pipes it to the
    `Build-Symlink` cmdlet to create the symbolic-link item on the filesystem.
     
.LINK
    New-Symlink
    Set-Symlink
    Remove-Symlink
    Build-Symlink
    about_Symlink
     
#>

function Get-Symlink
{
    [Alias("gsl")]
    
    [CmdletBinding(DefaultParameterSetName = "Specific")]
    param
    (
        
        # Tab completion.
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Specific")]
        [Alias("Name")]
        [string[]]
        $Names,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "All")]
        [switch]
        $All
        
    )
    
    begin
    {
        # Store the retrieved symlinks, to output together in one go at the end.
        $outputList = New-Object -TypeName System.Collections.Generic.List[Symlink]
    }
    
    process
    {
        if (-not $All)
        {
            # Read in the existing symlinks.
            $linkList = Read-Symlinks
            
            # Iterate through all the passed in names.
            foreach ($name in $Names)
            {
                # If the link doesn't exist, warn the user.
                $existingLink = $linkList | Where-Object { $_.Name -eq $name }
                if ($null -eq $existingLink)
                {
                    Write-Warning "There is no symlink named: '$name'."
                    continue
                }
                
                # Add the symlink object.
                $outputList.Add($existingLink)
            }
        }
        else
        {
            # Read in all of the symlinks.
            $outputList = Read-Symlinks
        }
    }
    
    end
    {
        # By default, this outputs in List formatting.
        $outputList | Sort-Object -Property Name
    }
}

<#
.SYNOPSIS
    Creates a new symlink.
     
.DESCRIPTION
    The `New-Symlink` cmdlet creates a new symlink definition, and optionally
    also creates the symbolic-link item on the filesystem.
     
.PARAMETER Name
    Specifies the name of the symlink to be created; must be unique.
     
.PARAMETER Path
    Specifies the path of the location of the symbolic-link item. If any parent
    folders in this path don't exist, they will be created.
     
.PARAMETER Target
    Specifies the path of the target which the symbolic-link item points to.
    This also defines whether the symbolic-link points to a directory or a file.
     
.PARAMETER CreationCondition
    Specifies a scriptblock to be used for this symlink. This scriptblock
    decides whether the symbolic-link item should be created on the filesystem.
    For detailed help, see the "CREATION CONDITION SCRIPTBLOCK" section in
    the help at: 'about_Symlink'.
     
.PARAMETER DontCreateItem
    Prevents the creation of the symbolic-link item on the filesystem.
    (The symlink definition will still be created).
     
.PARAMETER MoveExistingItem
    Specifies to move an already existing directory/file at the specifies path.
    This item will be moved to the specified target path rather than being
    deleted.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.PARAMETER Force
    Forces this cmdlet to create an symlink that writes over an existing one,
    and forces this cmdlet to create a symbolic-link item on the filesystem
    even if the creation condition evaluates to false.
     
    Even using this parameter, if the filesystem denies access to the necessary
    files, this cmdlet can fail.
     
.INPUTS
    None
     
.OUTPUTS
    Symlink
     
.NOTES
    For detailed help regarding the creation condition scriptblock, see
    the "CREATION CONDITION SCRIPTBLOCK" section in help at: 'about_Symlink'.
     
    This command is aliased by default to 'nsl'.
     
.EXAMPLE
    PS C:\> New-Symlink -Name "data" -Path ~\Documents\Data -Target D:\Files
     
    Creates a new symlink definition named "data", and also creates the
    symbolic-link item in the user's document folder under "Data", pointing to a
    location on the "D:\" drive.
     
.EXAMPLE
    PS C:\> New-Symlink -Name "data" -Path ~\Documents\Data -Target D:\Files
             -CreationCondition $script -DontCreateItem
     
    Creates a new symlink definition named "data", giving it a creation
    condition to be evaluated. However, this will not create the symbolic-link
    item on the filesystem due to the use of the '-DontCreateItem' switch.
     
.EXAMPLE
    PS C:\> New-Symlink -Name "program" -Path ~\Documents\Program
             -Target D:\Files\my_program -MoveExistingItem
                 
    Creates a new symlink definition named "program", and also creates the
    symbolic-link item in the user's document folder under the name "Program",
    pointing to a location on the "D:\" drive. By using the '-MoveExistingItem'
    switch, the "~\Documents\Program" folder will be moved into the "D:\Files"
    folder and renamed to "my_program".
     
.LINK
    Get-Symlink
    Set-Symlink
    Remove-Symlink
    about_Symlink
     
#>

function New-Symlink
{
    [Alias("nsl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true)]
        [string]
        $Name,
        
        [Parameter(Position = 1, Mandatory = $true)]
        [string]
        $Path,
        
        [Parameter(Position = 2, Mandatory = $true)]
        [string]
        $Target,
        
        [Parameter(Position = 3)]
        [scriptblock]
        $CreationCondition,
        
        [Parameter(Position = 4)]
        [switch]
        $MoveExistingItem,
        
        [Parameter(Position = 5)]
        [switch]
        $DontCreateItem,
        
        [Parameter()]
        [switch]
        $Force
        
    )
    
    # Validate that '-WhatIf'/'-Confirm' isn't used together with '-Force'.
    # This is ambiguous, so warn the user instead.
    Write-Debug "`$WhatIfPreference: $WhatIfPreference"
    Write-Debug "`$ConfirmPreference: $ConfirmPreference"
    if ($WhatIfPreference -and $Force)
    {
        Write-Error "You cannot specify both '-WhatIf' and '-Force' in the invocation for this cmdlet!"
        return
    }
    if (($ConfirmPreference -eq "Low") -and $Force)
    {
        Write-Error "You cannot specify both '-Confirm' and '-Force' in the invocation for this cmdlet!"
        return
    }
    
    # Validate that the name isn't empty.
    Write-Verbose "Validating parameters."
    if ([system.string]::IsNullOrWhiteSpace($Name))
    {
        Write-Error "The name cannot be blank or empty!"
        return
    }
    
    $expandedPath = [System.Environment]::ExpandEnvironmentVariables($Path)
    $expandedTarget = [System.Environment]::ExpandEnvironmentVariables($Target)
    
    # Validate that the target location exists. If the item isn't being moved there, check the full path,
    # otherwise check that the parent folder is valid.
    if (-not (Test-Path -Path $expandedTarget -ErrorAction Ignore) -and -not $MoveExistingItem)
    {
        Write-Error "The target path: '$Target' points to an invalid/non-existent location!"
        return
    }
    if (-not (Test-Path -Path (Split-Path -Path $expandedTarget -Parent) -ErrorAction Ignore) `
        -and $MoveExistingItem)
    {
        Write-Error "Part of the target path: '$(Split-Path -Path $expandedTarget -Parent)' is invalid!"
        return
    }
    
    # Validate that the name isn't already taken.
    $linkList = Read-Symlinks
    $existingLink = $linkList | Where-Object { $_.Name -eq $Name }
    if ($null -ne $existingLink)
    {
        if ($Force)
        {
            Write-Verbose "Existing symlink named: '$Name' exists, but since the '-Force' switch is present, the existing symlink will be deleted."
            $existingLink | Remove-Symlink
        }
        else
        {
            Write-Error "The name: '$Name' is already taken."
            return
        }
    }
    
    if ((Test-Path -Path $expandedPath -ErrorAction Ignore) -and $MoveExistingItem -and $PSCmdlet.ShouldProcess("Moving and renaming existing item from '$expandedPath' to '$expandedTarget'.", "Are you sure you want to move and rename the existing item from '$expandedPath' to '$expandedTarget'?", "Move File Prompt")) 
    {
        # Move the item over to the target parent folder, and rename it to the specified name given as part
        # of the target path.
        $fileName = Split-Path -Path $expandedPath -Leaf
        $newFileName = Split-Path -Path $expandedTarget -Leaf
        $targetFolder = Split-Path -Path $expandedTarget -Parent
        # Only troy to move the item if the parent folders differ, otherwise 'Move-Item' will thrown an error.
        if ((Split-Path -Path $expandedPath -Parent) -ne $targetFolder)
        {
            try
            {
                Move-Item -Path $expandedPath -Destination $targetFolder -Force -ErrorAction Stop -WhatIf:$false `
                    -Confirm:$false | Out-Null
            }
            catch
            {
                Write-Error "Could not move the existing item to the target destination.`nClose any programs which may be using this path and re-run the cmdlet."
                return
            }
        }
        
        # Only try to rename the item if the name differs, otherwise 'Rename-Item' will throw an error.
        if ($fileName -ne $newFileName)
        {
            try
            {
                Rename-Item -Path "$targetFolder\$filename" -NewName $newFileName -Force -ErrorAction Stop `
                    -WhatIf:$false -Confirm:$false | Out-Null
            }
            catch
            {
                Write-Error "Could not rename the existing item to match the target path.`nClose any programs which may be using this path and re-run the cmdlet."
                return
            }
        }
    }
    elseif (-not (Test-Path -Path $expandedPath -ErrorAction Ignore) -and $MoveExistingItem)
    {
        Write-Error "Cannot move the existing item from: '$expandedPath' because the location is invalid."
        return
    }
    
    # Create the object and save it to the database.
    Write-Verbose "Creating new symlink object."
    if ($null -eq $CreationCondition)
    {
        $newLink = [Symlink]::new($Name, $Path, $Target)
    }
    else
    {
        $newLink = [Symlink]::new($Name, $Path, $Target, $CreationCondition)
    }
    $linkList.Add($newLink)
    if ($PSCmdlet.ShouldProcess("Saving newly-created symlink to database at '$script:DataPath'.", "Are you sure you want to save the newly-created symlink to the database at '$script:DataPath'?", "Save File Prompt"))
    {
        Export-Clixml -Path $script:DataPath -InputObject $linkList -Force -WhatIf:$false -Confirm:$false `
            | Out-Null
    }
    
    # Build the symbolic-link item on the filesytem.
    if (-not $DontCreateItem -and ($newLink.TargetState() -eq "Valid") -and ($newLink.ShouldExist() -or $Force) -and $PSCmdlet.ShouldProcess("Creating symbolic-link item at '$expandedPath'.", "Are you sure you want to create the symbolic-link item at '$expandedPath'?", "Create Symbolic-Link Prompt"))
    {
        # Appropriately delete any existing items before creating the symbolic-link.
        $item = Get-Item -Path $expandedPath -ErrorAction Ignore
        # Existing item may be in use and unable to be deleted, so retry until the user has closed any
        # programs using the item.
        while (Test-Path -Path $expandedPath)
        {
            try
            {
                # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                # to; calling 'Delete()' will only destroy the symbolic-link iteself,
                # whilst calling 'Delete()' on a folder will not delete it's contents. Therefore check whether the
                # item is a symbolic-link to call the appropriate method.
                if ($null -eq $item.LinkType)
                {
                    Remove-Item -Path $expandedPath -Force -Recurse -ErrorAction Stop -WhatIf:$false `
                        -Confirm:$false | Out-Null
                }
                else
                {
                    $item.Delete()
                }
            }
            catch
            {
                Write-Error "The item located at: '$expandedPath' could not be deleted to make room for the symbolic-link."
                Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
            }
        }
        New-Item -ItemType SymbolicLink -Path $expandedPath -Value $expandedTarget -Force -WhatIf:$false `
            -Confirm:$false | Out-Null
    }
    
    Write-Output $newLink
}

<#
.SYNOPSIS
    Deletes a specified symlink item(s).
     
.DESCRIPTION
    The `Remove-YoutubeDlItem` cmdlet deletes one or more symlinks, specified
    by their name(s).
     
.PARAMETER Names
    Specifies the name(s) of the items to delete.
     
 [!]This parameter will autocomplete to valid symlink names.
     
.PARAMETER DontDeleteItem
    Prevents the deletion of the symbolic-link item from the filesystem.
    (The symlink definition will still be deleted).
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the symlinks
        to delete.
     
.OUTPUTS
    None
     
.NOTES
    This command is aliased by default to 'rsl'.
     
.EXAMPLE
    PS C:\> Remove-Symlink -Name "data"
     
    Deletes the symlink definition named "data", and deletes the symbolic-link
    item from the filesystem.
     
.EXAMPLE
    PS C:\> Remove-Symlink -Names "data","files"
     
    Deletes the symlink definitions named "data" and "files", and their
    symbolic-link items from the filesystem.
     
.EXAMPLE
    PS C:\> Remove-Symlink -Name "data" -DontDeleteItem
     
    Deletes the symlink definition named "data", but does not delete the
    symbolic-link item from the filesystem; that remains unchanged.
     
.LINK
    New-Symlink
    Get-Symlink
    Set-Symlink
    about_Symlink
     
#>

function Remove-Symlink
{
    [Alias("rsl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        # Tab completion.
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("Name")]
        [string[]]
        $Names,
        
        [Parameter(Position = 1)]
        [switch]
        $DontDeleteItem
        
    )
    
    process
    {
        # Read in the existing symlinks.
        $linkList = Read-Symlinks
        
        foreach ($name in $Names)
        {
            # If the link doesn't exist, warn the user.
            $existingLink = $linkList | Where-Object { $_.Name -eq $name }
            if ($null -eq $existingLink)
            {
                Write-Error "There is no symlink named: '$name'."
                continue
            }
            
            # Delete the symlink from the filesystem.
            $expandedPath = $existingLink.FullPath()
            $item = Get-Item -Path $expandedPath -ErrorAction Ignore
            if (-not $DontDeleteItem -and $existingLink.Exists() -and $PSCmdlet.ShouldProcess("Deleting symbolic-link at '$expandedPath'.", "Are you sure you want to delete the symbolic-link at '$expandedPath'?", "Delete Symbolic-Link Prompt"))
            {
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself.
                        $item.Delete()
                    }
                    catch
                    {
                        Write-Error "The symbolic-link located at: '$expandedPath' could not be deleted."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
            }
            
            # Remove the link from the list.
            Write-Verbose "Deleting the symlink object."
            $linkList.Remove($existingLink) | Out-Null
        }
        
        # Save the modified database.
        if ($PSCmdlet.ShouldProcess("Updating database at '$script:DataPath' with the changes (deletions).", "Are you sure you want to update the database at '$script:DataPath' with the changes (deletions)?", "Save File Prompt"))
        {
            Export-Clixml -Path $script:DataPath -InputObject $linkList -Force -WhatIf:$false `
                -Confirm:$false | Out-Null
        }
    }
}

<#
.SYNOPSIS
    Changes a value of a symlink item.
     
.DESCRIPTION
    The `Set-Symlink` cmdlet changes the value of a symlink.
     
.PARAMETER Name
    Specifies the name of the symlink to be changed.
     
 [!]This parameter will autocompleted to valid names for a symlink.
 
.PARAMETER Property
    Specifies the name of the property to change.
     
 [!]This parameter will autocompleted to the following: "Name", "Path",
    "Target", "CreationCondition".
     
.PARAMETER Value
    Specifies the new value of the property being changed.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.PARAMETER Force
    Forces this cmdlet to change the name of a symlink even if it overwrites an
    existing one, or forces this cmdlet to create a symbolic-link item on the
    filesystem even if the creation condition evaluates to false.
     
    Even using this parameter, if the filesystem denies access to the necessary
    files, this cmdlet can fail.
     
.INPUTS
    System.String
        You can pipe the name of the symlink to change.
     
.OUTPUTS
    None
     
.NOTES
    For detailed help regarding the creation condition scriptblock, see
    the "CREATION CONDITION SCRIPTBLOCK" section in help at: 'about_Symlink'.
     
    This command is aliased by default to 'ssl'.
     
.EXAMPLE
    PS C:\> Set-Symlink -Name "data" -Property "Name" -Value "WORK"
     
    Changes the name of a symlink definition named "data", to the new name
    of "WORK". From now on, there is not symlink named "data" anymore, and that
    name is free for future use.
     
.EXAMPLE
    PS C:\> Set-Symlink -Name "data" -Property "Path" -Value "~\Desktop\Files"
     
    Changes the path of the symlink definition named "data", to a new value
    located in the user's desktop folder. The old symbolic-link item at the
    previous location will be deleted from the filesystem, and a new item will
    be created at the new location.
 
.EXAMPLE
    PS C:\> Set-Symlink -Name "data" -Property "Target" -Value "D:\new\target"
     
    Changes the target of the symlink definition named "data", to a new value
    on the "D:\" drive. The existing symbolic-link item on the filesystem will
    have its target updated to this new value, (technically involves deleting
    and re-creating the item since the target cannot be modified).
     
.EXAMPLE
    PS C:\> Set-Symlink -Name "data" -Property "CreationCondition"
             -Value { return $false }
              
    Changes the creation condition of the symlink definition named "data", to
    a new scriptblock which always returns $FALSE. This will not delete the
    existing symbolic-link item on the filesystem, even though if the condition
    was evaluated now, it would return false.
     
.LINK
    Get-Symlink
    Set-Symlink
    Remove-Symlink
    about_Symlink
     
#>

function Set-Symlink
{
    [Alias("ssl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        # Tab completion.
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName)]
        [string]
        $Name,
        
        [Parameter(Position = 1, Mandatory = $true)]
        [ValidateSet("Name", "Path", "Target", "CreationCondition")]
        [string]
        $Property,
        
        [Parameter(Position = 2, Mandatory = $true)]
        [AllowNull()]
        $Value,
        
        [Parameter()]
        [switch]
        $Force
        
    )
    
    process
    {
        # If the link doesn't exist, warn the user.
        $linkList = Read-Symlinks
        $existingLink = $linkList | Where-Object { $_.Name -eq $Name }
        if ($null -eq $existingLink)
        {
            Write-Error "There is no symlink named: '$Name'."
            return
        }
        
        # Modify the property values.
        Write-Verbose "Validating parameters."
        if ($Property -eq "Name")
        {
            # Validate that the new name is valid.
            if ([system.string]::IsNullOrWhiteSpace($Name))
            {
                Write-Error "The new name cannot be blank or empty!"
                return
            }
            # Validate that the new name isn't already taken.
            $clashLink = $linkList | Where-Object { $_.Name -eq $Value }
            if ($null -ne $clashLink)
            {
                if ($Force)
                {
                    Write-Verbose "Existing symlink named: '$Value' exists, but since the '-Force' switch is present, the existing symlink will be deleted."
                    $clashLink | Remove-Symlink
                }
                else
                {
                    Write-Error "The name: '$Value' is already taken!"
                    return
                }
            }
            
            $linkList = Read-Symlinks
            $existingLink = $linkList | Where-Object { $_.Name -eq $Name }
            
            $existingLink.Name = $Value
        }
        elseif ($Property -eq "Path")
        {
            # Validate the new path isn't empty.
            if ([System.String]::IsNullOrWhiteSpace($Value))
            {
                Write-Error "The new path cannot be blank or empty!"
                return
            }
            # Validate that the target exists.
            if (-not (Test-Path -Path ([System.Environment]::ExpandEnvironmentVariables($existingLink.FullTarget())) `
                    -ErrorAction Ignore))
            {
                Write-Error "The symlink's target path: '$($existingLink.FullTarget())' points to an invalid location!"
                return
            }
            
            # Firstly, delete the symlink from the filesystem at the original path location.
            $expandedPath = $existingLink.FullPath()
            $item = Get-Item -Path $expandedPath -ErrorAction Ignore
            if ($existingLink.Exists() -and $PSCmdlet.ShouldProcess("Deleting old symbolic-link at '$expandedPath'.", "Are you sure you want to delete the old symbolic-link at '$expandedPath'?", "Delete Symbolic-Link Prompt"))
            {
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself.
                        $item.Delete()
                    }
                    catch
                    {
                        Write-Error "The old symbolic-link located at '$expandedPath' could not be deleted."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
            }
            
            # Then change the path property, and re-create the symlink at the new location, taking into account
            # that there may be existing items at the new path.
            $existingLink._Path = $Value
            $expandedPath = $existingLink.FullPath()
            if (($existingLink.ShouldExist() -or $Force) -and ($existingLink.TargetState() -eq "Valid") -and $PSCmdlet.ShouldProcess("Creating new symbolic-link item at '$expandedPath'.", "Are you sure you want to create the new symbolic-link item at '$expandedPath'?", "Create Symbolic-Link Prompt"))
            {
                # Appropriately delete any existing items before creating the symbolic-link.
                $item = Get-Item -Path $expandedPath -ErrorAction Ignore
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself,
                        # whilst calling 'Delete()' on a folder will not delete it's contents. Therefore check
                        # whether the item is a symbolic-link to call the appropriate method.
                        if ($null -eq $item.LinkType)
                        {
                            Remove-Item -Path $expandedPath -Force -Recurse -ErrorAction Stop -WhatIf:$false `
                                -Confirm:$false | Out-Null
                        }
                        else
                        {
                            $item.Delete()
                        }
                    }
                    catch
                    {
                        Write-Error "The item located at '$expandedPath' could not be deleted to make room for the symbolic-link."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
                
                New-Item -ItemType SymbolicLink -Path $existingLink.FullPath() -Value $existingLink.FullTarget() `
                    -Force -WhatIf:$false -Confirm:$false | Out-Null
            }
        }
        elseif ($Property -eq "Target")
        {
            # Validate that the target exists.
            if (-not (Test-Path -Path ([System.Environment]::ExpandEnvironmentVariables($Value)) `
                    -ErrorAction Ignore))
            {
                Write-Error "The new target path: '$Value' points to an invalid/non-existent location!"
                return
            }
            
            # Firstly, delete the symlink with the old target value from the filesystem.
            $expandedPath = $existingLink.FullPath()
            $item = Get-Item -Path $expandedPath -ErrorAction Ignore
            if ($existingLink.Exists() -and $PSCmdlet.ShouldProcess("Deleting outdated symbolic-link at '$expandedPath'.", "Are you sure you want to delete the outdated symbolic-link at '$expandedPath'?", "Delete Symbolic-Link Prompt"))
            {
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself.
                        $item.Delete()
                    }
                    catch
                    {
                        Write-Error "The outdated symbolic-link located at '$expandedPath' could not be deleted."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
            }
            
            # Then change the target property, and re-create the symlink at the with the new target,
            # taking into account that there may be existing items at the new path.
            $existingLink._Target = $Value
            $expandedPath = $existingLink.FullPath()
            if (($existingLink.ShouldExist() -or $Force) -and ($existingLink.TargetState() -eq "Valid") -and $PSCmdlet.ShouldProcess("Creating new symbolic-link item at '$expandedPath'.", "Are you sure you want to create the new symbolic-link item at '$expandedPath'?", "Create Symbolic-Link Prompt"))
            {
                # Appropriately delete any existing items before creating the symbolic-link.
                $item = Get-Item -Path $expandedPath -ErrorAction Ignore
                # Existing item may be in use and unable to be deleted, so retry until the user has closed
                # any programs using the item.
                while (Test-Path -Path $expandedPath)
                {
                    try
                    {
                        # Calling 'Remove-Item' on a symbolic-link will delete the original items the link points
                        # to; calling 'Delete()' will only destroy the symbolic-link iteself,
                        # whilst calling 'Delete()' on a folder will not delete it's contents. Therefore check
                        # whether the item is a symbolic-link to call the appropriate method.
                        if ($null -eq $item.LinkType)
                        {
                            Remove-Item -Path $expandedPath -Force -Recurse -ErrorAction Stop -WhatIf:$false `
                                -Confirm:$false | Out-Null
                        }
                        else
                        {
                            $item.Delete()
                        }
                    }
                    catch
                    {
                        Write-Error "The item located at '$expandedPath' could not be deleted to make room for the symbolic-link."
                        Read-Host -Prompt "Close any programs using this path, and enter any key to retry"
                    }
                }
                New-Item -ItemType SymbolicLink -Path $existingLink.FullPath() -Value $existingLink.FullTarget() `
                    -Force -WhatIf:$false -Confirm:$false | Out-Null
            }
        }
        elseif ($Property -eq "CreationCondition")
        {
            $existingLink._Condition = $Value
        }
        
        if ($PSCmdlet.ShouldProcess("Updating database at '$script:DataPath' with the changes.", "Are you sure you want to update the database at '$script:DataPath' with the changes?", "Save File Prompt"))
        {
            Export-Clixml -Path $script:DataPath -InputObject $linkList -WhatIf:$false -Confirm:$false `
                | Out-Null
        }
    }
}

# Tab expansion assignements for commands.
$argCompleter_SymlinkName =
{
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    
    # Import all objects from the database file.
    $linkList = Read-Symlinks
    
    if ($linkList.Count -eq 0)
    {
        Write-Output ""
    }
    
    # Return the names which match the currently typed in pattern.
    # This first strips the string of any quotation marks, then matches it to the valid names,
    # and then inserts the quotation marks again. This is necessary so that strings with spaces have quotes,
    # otherwise they will not be treated as one parameter.
    $linkList.Name | Where-Object { $_ -like "$($wordToComplete.Replace(`"`'`", `"`"))*" } | ForEach-Object { "'$_'" }
    
}

Register-ArgumentCompleter -CommandName Get-Symlink -ParameterName Names -ScriptBlock $argCompleter_SymlinkName
Register-ArgumentCompleter -CommandName Set-Symlink -ParameterName Name -ScriptBlock $argCompleter_SymlinkName
Register-ArgumentCompleter -CommandName Remove-Symlink -ParameterName Names -ScriptBlock $argCompleter_SymlinkName
Register-ArgumentCompleter -CommandName Build-Symlink -ParameterName Names -ScriptBlock $argCompleter_SymlinkName
#endregion Load compiled code