Skip to content

Commit 058b1c4

Browse files
committed
Update of 2020-12-18
- New file: Demo - Message boxes in PowerShell.ps1 - New file: Functions library.psm1 - Fix typo in the name of Download-Channel9VideosFromRSS.ps1 - Rename: README.md to README.markdown - Update README.markdown
1 parent a65867f commit 058b1c4

File tree

5 files changed

+165
-26
lines changed

5 files changed

+165
-26
lines changed

(Code snippets)/Administrative privileges.psm1

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
$TitleBar = Split-Path -Path $PSCommandPath -Leaf
2+
3+
$Method1 = 'System.Windows.Forms.MessageBox'
4+
$Method2 = 'Microsoft.VisualBasic.Interaction.MsgBox()'
5+
$Method3 = 'WScript.Shell.Popup()'
6+
7+
$MessageBase = "This message box is brought to you by:`n{0}`nHere are some Unicode characters:`n😀 ā ی ± ≠ 👩"
8+
$Message1 = $MessageBase -f $Method1
9+
$Message2 = $MessageBase -f $Method2
10+
$Message3 = $MessageBase -f $Method3
11+
12+
# Load System.Windows.Forms and enable visual styles
13+
Write-Output 'Loading System.Windows.Forms'
14+
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
15+
16+
# Enable visual styles
17+
# This command is required on Windows XP and later (yes, even on Windows 10)
18+
# Without this, your message boxes look like something form Windows 95
19+
Write-Output 'Enabling visual styles'
20+
[System.Windows.Forms.Application]::EnableVisualStyles()
21+
22+
<# METHOD 1:
23+
24+
System.Windows.Forms.MessageBox
25+
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox
26+
27+
Comforms with visual styles
28+
29+
To get the details of parameter types:
30+
[enum]::GetValues([System.Windows.Forms.MessageBoxButtons])
31+
[enum]::GetValues([System.Windows.Forms.MessageBoxIcon])
32+
To get the details of the return type:
33+
[enum]::GetValues([System.Windows.Forms.DialogResult])
34+
#>
35+
Write-Output "Demonstrating method 1: $Method1"
36+
$result = [System.Windows.Forms.MessageBox]::Show($Message1, $TitleBar , 'OK', 'Asterisk');
37+
Write-Output $result
38+
39+
<# METHOD 2:
40+
41+
Microsoft.VisualBasic.Interaction.MsgBox()
42+
https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.interaction.msgbox
43+
44+
Comforms with visual styles
45+
46+
To get the details of parameter type:
47+
[enum]::GetValues([Microsoft.VisualBasic.MsgBoxStyle])
48+
To get the details of the return type:
49+
[enum]::GetValues([Microsoft.VisualBasic.MsgBoxResult])
50+
#>
51+
Write-Output "Demonstrating method 2: $Method2"
52+
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
53+
$result = [Microsoft.VisualBasic.Interaction]::MsgBox($Message2, 'OKOnly,Information', $TitleBar )
54+
Write-Output $result
55+
56+
<# METHOD 3 (DEPRECATED):
57+
58+
WScript.Shell.Popup()
59+
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/x83z1d9f(v=vs.84)
60+
61+
Not affected by visual styles
62+
(Looks like something from Windows 95!)
63+
#>
64+
Write-Output "Demonstrating method 3: $Method3"
65+
$MsgBox = New-Object -ComObject wscript.shell
66+
$result = $MsgBox.Popup($Message3, 0, $TitleBar, 0 -bor 64)
67+
Write-Output $result
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function AmIAdmin {
2+
<#
3+
.SYNOPSIS
4+
Returns $True when the process running this script has administrative privileges
5+
.DESCRIPTION
6+
Starting with PowerShell 4.0, the "Requires -RunAsAdministrator" directive
7+
prevents the execution of the script when administrative privileges are
8+
absent. However, there are still times that you'd like to just check the
9+
privilege (or lack thereof), e.g., to announce it to the user or downgrade
10+
script functionality gracefully.
11+
.NOTES
12+
For the Requires directive, see the "about_Requires" help page.
13+
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-7
14+
#>
15+
$MyId = [System.Security.Principal.WindowsIdentity]::GetCurrent()
16+
$WindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal( $MyId )
17+
return $WindowsPrincipal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator )
18+
}
19+
20+
function Unregister-ScheduledTaskEx {
21+
<#
22+
.SYNOPSIS
23+
Unregisters several scheduled tasks whose names matches a wildcard patten (not regex)
24+
.DESCRIPTION
25+
Uses Get-ScheduledTask to get a list of all scheduled tasks, filters then via the -like operator, and runs Unregister-ScheduledTask against the resulting set.
26+
Extremly dangerous. Use with caution.
27+
.EXAMPLE
28+
PS C:\> Unregister-ScheduledTaskEx -TaskNameEx "AppThatIJustUninstalled_User.*"
29+
Removes scheduled tasks whose names begins with "AppThatIJustUninstalled_User."
30+
.INPUTS
31+
None
32+
.OUTPUTS
33+
None
34+
.NOTES
35+
Version 1.0
36+
#>
37+
[CmdletBinding()]
38+
param (
39+
[Parameter(Mandatory)]
40+
[SupportsWildcards()]
41+
[String[]]
42+
$TaskNameEx
43+
)
44+
$MatchingTasks = Get-ScheduledTask | Where-Object -FilterScript { foreach ($item in $TaskNameEx) { if ($_.TaskName -like $item) { return $true } } }
45+
if ($null -ne $MatchingTasks) {
46+
# This command does not seem to respect the $VerbosePreference and asks for confirmation anyway
47+
# Add -Confirm:$false to make it stop
48+
Unregister-ScheduledTask -TaskName $MatchingTasks.TaskName
49+
} else {
50+
Write-Verbose "Found no scheduled tasks matching the requested criteria"
51+
}
52+
}
53+
54+
function Remove-RegistryValues {
55+
<#
56+
.SYNOPSIS
57+
Attempts to remove one or more values from a given path in Windows Registry.
58+
.DESCRIPTION
59+
Removes one or more specified values from a given path in Windows Registry, if they exist. Remains silent if they don't exist. Generates a warning in the even of other problems.
60+
.EXAMPLE
61+
PS C:\> Remove-RegistryValues -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "AppThatIJustUninstalled-TrayIcon", "AppThatIJustUninstalled-Updater"
62+
Opens the "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" path of Windows Registry, looks for two values: "AppThatIJustUninstalled-TrayIcon", "AppThatIJustUninstalled-Updater". If they exist, deletes them.
63+
.INPUTS
64+
None
65+
.OUTPUTS
66+
None
67+
.NOTES
68+
Version 1.0
69+
#>
70+
[CmdletBinding()]
71+
param (
72+
[Parameter(Mandatory, Position = 0)] [String] $Path,
73+
[Parameter(Mandatory, Position = 1)] [String[]] $Name
74+
)
75+
if (Test-Path -Path $Path) {
76+
Remove-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue -ErrorVariable ee
77+
foreach ($e in $ee) {
78+
if (-not ($e.Exception -is [System.ArgumentException])) { Write-Warning $e }
79+
}
80+
} else {
81+
Write-Verbose "Could not find '$Path'"
82+
}
83+
}

README.md renamed to README.markdown

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Scripts for carrying out administrative tasks, mostly in Windows
77
Deals with Microsoft Store app packages.
88

99
1. `Inventory AppX Packages.ps1`: The `Get-AppxPackage` cmdlet in PowerShell can find all installed Microsoft Store apps, but it does not do a good job of discovering their display names. At best, it can show the technical package name. This script lists all AppX packages installed for the current user account, along with their display names. It accepts a `-Verbose` switch and its output can be piped to `Format-Table` or `Format-List`. I believe it can be expanded to work machine-wide.
10-
2. `Reinstall-AppxPackages.ps1`: This script belongs to long-gone days. Nowdays, it is probably just dangerous. It is used to reset and re-register all AppX packages that were either shipped with Windows or were published by Microsoft. It is a safer alternative to the utterly sadistic oneliner that retrived a list of all AppX packages and reset them all.
11-
3. `Repair system apps.ps1`: This script also belongs to long-gone days. Nowdays, it is probably just dangerous. It re-registers all AppX packages installed in the "SystemApp" folder.
12-
4. `(Specialized) Remove these appx packages.ps1`: This one is mostly for me, but others can find educational value in it. This script uninstalls a number of Microsoft Store apps for all users, without deleting their provisioned packages. I believe it can only run on Windows 10 version 1809 or later.
10+
2. `Reinstall-AppxPackages.ps1`: This script belongs to long-gone days. Nowadays, it is probably just dangerous. It resets and re-registers all AppX packages that were either shipped with Windows or were published by Microsoft. It is a safer alternative to the utterly sadistic one-liner that retrieves a list of all AppX packages and reset them all.
11+
3. `Repair system apps.ps1`: This script also belongs to long-gone days. Nowadays, it is probably just dangerous. It re-registers all AppX packages installed in the "SystemApp" folder.
12+
4. `(Specialized) Remove these AppX packages.ps1`: This one is mostly for me, but others can find educational value in it. This script uninstalls a number of Microsoft Store apps for all users, without deleting their provisioned packages. I believe it can only run on Windows 10 version 1809 or later.
1313

1414
## BITS
1515

@@ -21,7 +21,7 @@ Deals with Microsoft Store app packages.
2121

2222
## Download
2323

24-
1. `Download-Channl9VideosFromRSS.ps1` helps download entire Channel 9 sets of videos. It accepts one RSS URL for the video set and one download location.
24+
1. `Download-Channel9VideosFromRSS.ps1` helps download entire Channel 9 sets of videos. It accepts one RSS URL for the video set and one download location.
2525

2626
## Firewall
2727

@@ -31,19 +31,19 @@ Deals with Microsoft Store app packages.
3131

3232
There was a period of time when Windows was plagued with bugs that corrupted the icon cache. These scripts were conceived in that time, as ways of mitigating the problem. I have long stopped using them though.
3333

34-
`Refresh icon cache with MoveFile.cmd` is the most effective of those but it is hard-coded to use an installed copy of `movefile.exe` from the Microsoft Sysinternals utility set.
34+
`Refresh icon cache with MoveFile.cmd` is the most effective of those but it is hard-coded to use an installed copy of `MoveFile.exe` from the Microsoft Sysinternals utility set.
3535

3636
## Last logon time
3737

38-
`lastlogon.vbs` takes a long time to run, but returns a list of all local users and the date and time of their last logon action.
38+
`LastLogon.vbs` takes a long time to run, but returns a list of all local users and the date and time of their last logon action.
3939

4040
A matter of licensing: I did not write this script. The user who posted it was called `Corvus1` and posted it in educational spirit.
4141

4242
## Maintenance
4343

4444
1. `Repair-AllVolumes.ps1`: Enumerates all fixed-disk volumes and sequentially runs `Repair-Volume` on them to scan them for errors.
4545
2. `Repair-Windows.ps1`: Repairs the online Windows instance by running DISM and SFC. Their logs are moved to the desktop.
46-
3. `NGEN Update.bat`: Runs NGEN inside Windows PowerShell. While PowerShell 6 and 7 run on .NET Core, Windows PowerShell and some Windows-exclusive PowerShell modules (which PowerShell 7 also loads) run on .NET Framework. Run this script with admin privileges whenever you update .NET Framework, or whenever you feel Windows PowerShell or PowerShell 7 for Windows are sluggish at launch time.
46+
3. `nGen Update.bat`: Runs nGen inside Windows PowerShell. While PowerShell 6 and 7 run on .NET Core, Windows PowerShell and some Windows-exclusive PowerShell modules (which PowerShell 7 also loads) run on .NET Framework. Run this script with admin privileges whenever you update .NET Framework, or whenever you feel Windows PowerShell or PowerShell 7 for Windows are sluggish at launch time.
4747

4848
## Security
4949

@@ -90,7 +90,7 @@ The names of these scripts are self-explanatory.
9090

9191
## Time
9292

93-
1. `Firmware time is UTC.reg`: Configures Windows to interpret the real-time clock as UTC time. Ordinarily, Windows interprets it as the local time correponding to the time zone you've selected. Most Linux distros that interpret it as UTC, so you may need this script if you multi-boot a Linux distro along Windows. After applying this, restart the computer.
93+
1. `Firmware time is UTC.reg`: Configures Windows to interpret the real-time clock as UTC time. Ordinarily, Windows interprets it as the local time corresponding to the time zone you've selected. Most Linux distros that interpret it as UTC, so you may need this script if you multi-boot a Linux distro along Windows. After applying this, restart the computer.
9494

9595
## Unicode test suite
9696

@@ -112,3 +112,10 @@ The following two scripts are co-developed with Ramesh Srinivasan (the author of
112112

113113
1. Find current wallpaper (Windows 7).ps1
114114
2. Find current wallpaper (Windows 8,10).ps1
115+
116+
## (Code snippets)
117+
118+
This special folder contains source code that one should not run directly. Rather, PowerShell developers could read them, learn from them, or include portions of them in their PowerShell scripts.
119+
120+
1. `Function library.psm1`: Contains a number of reusable functions. I've chosen the `.psm1` filename extension to prevent it from being accidentally run as a script. Each function has its own local help.
121+
2. `Demo - Message boxes in PowerShell.ps1`: This scripts demonstrates how to invoke message boxes within a PowerShell script. Normally, one must not use message boxes (or `Write-Host`) inside PowerShell scripts. Before Windows 10, however, console apps had serious problems displaying Unicode characters, so I used message boxes instead.

0 commit comments

Comments
 (0)