-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-defaultprofile-startmenu-shortcuts.ps1
90 lines (73 loc) · 2.06 KB
/
generate-defaultprofile-startmenu-shortcuts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Filename: generate-defaultprofile-startmenu-shortcuts.ps1
# Description: Generates executable shortcuts within Start Menu for the Default User.
# Author(s): Alex Portell <github.com/portellam>
#
function main()
{
$default_username = "Default"
$username = $Env:UserName
$username_reference = '%username%'
$appdata_path = "C:\Users\$default_username\AppData"
$local_path = "$appdata_path\Local\*\*"
$roaming_path = "$appdata_path\Roaming\*\*"
Write-Host "Creating Start Menu shortcuts for the Default User..."
if(Create-Shortcuts-From-Path($local_path) -ne 0)
{
Write-Host "Failure."
exit 1
}
if(Create-Shortcuts-From-Path($roaming_path) -ne 0)
{
Write-Host "Failure."
exit 1
}
Write-Host "Success."
exit 0
}
function Create-Shortcuts-From-Path($path)
{
$has_passed_once = $false
$startmenu_path = "$appdata_path\Roaming\Microsoft\Windows\Start Menu"
$path_results = New-Object System.Collections.ArrayList
$shell = New-Object -ComObject WScript.Shell
foreach($file in Get-ChildItem $path)
{
if(-not($file -like "*.exe"))
{
continue
}
$path_results.Add($file) *>$null
}
if($path_results.Count -lt 1)
{
return 0
}
foreach($file in $path_results)
{
try
{
$file_base_dir = (Get-Item $file).Directory.Name
$shortcut_dir = "$startmenu_path\$file_base_dir"
$shortcut_path = "$shortcut_dir\" + $file.BaseName + ".lnk"
$default_username_temp = "\$default_username\"
$username_reference_temp = "\$username_reference\"
$shortcut_target = $file.FullName.Replace($default_username_temp, $username_reference_temp)
$shortcut = $shell.CreateShortCut($shortcut_path)
$shortcut.TargetPath = $shortcut_target
New-Item -ItemType Directory -Path $shortcut_dir *>$null
$shortcut.Save()
$has_passed_once = $true
}
catch
{
Write-Host "An error occured:"
Write-Host $_.ScriptStackTrace
}
}
if($has_passed_once -eq $false)
{
return 1
}
return 0
}
main