- KitchenSink
Custom functions created that have helped me along the way.
Just a simple way to toss a system-controlled yes/no question to the user.
Get-AskUserYNQuestion -Question 'Are you ready?'Are you ready?
Choices:
[Y] Yes [N] No [?] Help (default is "Y"): y
Yes
Prompt the user to select a folder in a file browser window.
Get-FolderPrompt the user to select a CSV file in a file browser to obtain a path to a CSV file.
Get-CSVFilePathThis function is a wrapper for PSWriteHTML. It will prompt the user with a file browser to select a CSV to convert into an HTML report.
Convert-CSVtoHTMLMust be executed in PowerShell administrator mode.
The Install-CustomModule function simplifies the process of installing PowerShell modules by automating the following steps:
- Copying the module to the appropriate module directory based on the
UserLevelparameter. - Adding the module to the user's PowerShell profile for automatic loading in future sessions.
- Optionally unblocking unsigned scripts if the
-Unblockparameter is specified.
-InputDir: Specifies the path of the module to install. This is a mandatory parameter and should point to the folder containing the module files.-UserLevel: Specifies the level of the user for which the module should be installed. Valid values are:'Single': Installs the module for the current user only.'All': Installs the module for all users on the system. The default value is'All'.
-Unblock: Optional switch to unblock the module script if it is not signed.
# Install the module for all users
Install-CustomModule -InputDir "C:\Path\To\Module" -UserLevel "All"
# Install the module for the current user only
Install-CustomModule -InputDir "C:\Path\To\Module" -UserLevel "Single"
# Install the module for all users and unblock unsigned scripts
Install-CustomModule -InputDir "C:\Path\To\Module" -UserLevel "All" -Unblock- The function ensures the module is imported into the user's PowerShell profile for automatic loading in future sessions. If the profile does not exist, it will create one.
- If submodules (additional
.psm1files) are found within the module directory, they will also be added to the profile and imported. - The function validates the installation by checking if the module and its commands are successfully imported.
- If the
-Unblockparameter is used, the function will check the script's signature and unblock it if it is not signed.
The Join-FunctionsToPSM function creates a single PowerShell module file (.psm1) by combining all .ps1 files from a specified folder. It ensures that the resulting .psm1 file is well-structured and includes all the functions from the specified folder. Each function definition in the .psm1 file is separated by two blank lines for readability.
- Combines all
.ps1files in the specified folder into a single.psm1file. - Automatically creates a backup of an existing
.psm1file with a.bakextension before overwriting it. - Validates the existence of the root directory and functions directory.
- Ensures the
.psm1file is encoded in UTF-8 for compatibility.
-RootDir: Specifies the root folder path of the module/project. This is the folder where the.psm1file will be created. The name of the.psm1file will match the name of this folder.-FunctionsDir: Specifies the name of the folder within the root directory that contains the.ps1function files. The default value is"Functions". This parameter can be a relative or absolute path.
# Combine all .ps1 files in the "Functions" folder into a single .psm1 file
Join-FunctionsToPSM -RootDir "C:\Github\ProjectSample" -FunctionsDir "Functions"
# Combine all .ps1 files in a custom folder into a single .psm1 file
Join-FunctionsToPSM -RootDir "D:\Projects\MyModule" -FunctionsDir "CustomFunctions"- If no
.ps1files are found in the specified functions directory, the function will throw an error. - If a
.psm1file with the same name already exists, it will be backed up with a.bakextension. If a.bakfile already exists, it will be replaced with the latest backup. - The resulting
.psm1file will include all functions, separated by two blank lines for readability. - The function validates the paths provided for the root directory and functions directory to ensure they exist.
- The
.psm1file is written with UTF-8 encoding to ensure compatibility across systems.
The New-CredsTxtFile function is used to create a secure password file or validate a password against an existing file.
To create a new password file, use the -Filepath parameter to specify the path where the password file will be saved. The function will prompt you to enter a password, which will then be saved to the specified file.
New-CredsTxtFile -Filepath "C:\creds\creds.txt"To validate a password against an existing file, use the -Filepath parameter to specify the path of the password file and the -Validate parameter. The function will prompt you to enter a password, which will then be compared to the password in the specified file.
New-CredsTxtFile -Filepath "C:\creds\creds.txt" -ValidateNew-CredsTxtFile -Filepath "C:\creds\creds.txt": New credential file created.New-CredsTxtFile -Filepath "C:\creds\creds.txt" -Validate: No new file created; validates the password provided against the encrypted password in the file.
Check your PSGallery installed modules for updates. Returns modules that are outdated with links.
Get-ModuleUpdatesName : Carbon
UpdateAvailable : True
InstalledVersion : 2.13.0
Version : 2.15.1
PublishedDate : 8/18/2023 6:53:24 PM
Link : https://www.powershellgallery.com/packages/Carbon
Name : PSSharedGoods
UpdateAvailable : True
InstalledVersion : 0.0.264
Version : 0.0.266
PublishedDate : 9/18/2023 6:20:41 AM
Link : https://www.powershellgallery.com/packages/PSSharedGoods
Convert timestamps from Windows Time File or UNIX into a readable format.
Convert-TimeStamp -Timestamp 1282713827429687506/23/2007 10:57:54 PM
This function encrypts each parameter value and exports the key-value pairs into a JSON file at the -FilePath location. It also has static parameter values that can be selected. If custom values are needed, use the New-SecuredJSON version.
New-SecuredJSONStatic -Filepath "D:\Code\test5.json" -Password "P@ssw0rd" -Username "MyUsername" -Email "jdoe@sample.com"Outputs a JSON file like this (encrypted values replaced with "<Encrypted>"):
{
"Username": "<Encrypted>",
"Email": "<Encrypted>",
"Password": "<Encrypted>"
}This function has dynamic parameters. To execute it, pass the -FilePath and at least one -Params. It encrypts the parameter values and outputs the results into a JSON file.
$params = @{
"Password" = "P@ssw0rd"
"Username" = "MyUsername"
"StudentID" = "568566"
}
New-SecuredJSON -Filepath "D:\Code\test4.json" -Params $paramsOutputs a JSON file like this (encrypted values replaced with "<Encrypted>"):
{
"Username": "<Encrypted>",
"Password": "<Encrypted>",
"StudentID": "<Encrypted>"
}Reads the secured JSON file created with New-SecuredJSONStatic or New-SecuredJSON and converts the encrypted values into plain text.
$data = Read-SecuredJSON -Path "D:\Code\test4.json"
$Password = $data.Password$Data = Read-SecuredJSON -Path "D:\Code\test4.json"
$Password = $data.Password | ConvertTo-SecureString -AsPlainText -ForceThe Invoke-Speech function speaks the specified text using the default system settings. You can specify the speed, volume, and voice of the speech, and you can generate a PowerShell script that reproduces the speech settings.
# This example speaks the text "Hello, world!" using the default system settings.
Invoke-Speech -Text "Hello, world!"# This example speaks the text "Hello, world!" using the Microsoft David Desktop voice, at a speed of 200 words per minute and a volume of 50%.
Invoke-Speech -Text "Hello, world!" -Speed 200 -Volume 50 -Voice "Microsoft David Desktop"# This example speaks the text "Hello, world!" and generates a PowerShell script that reproduces the speech settings.
Invoke-Speech -Text "Hello, world!" -Generate# This example speaks the text "Hello, world!" and returns a hash table of the speech settings.
Invoke-Speech -Text "Hello, world!" -ResumeDownload a GitHub project as a .zip and decompress the files to a specific path.
Get-GithubProject -Url "https://github.com/EReis0/PowerShell-Modules/archive/refs/heads/main.zip" -Output "C:\Users\jdoe\Documents\"The Convert-TOTPToMFA function generates a Time-based One-Time Password (TOTP) code based on the provided secret, period, number of digits, and algorithm. It is useful for generating MFA (Multi-Factor Authentication) codes.
-Secret: The Base32 encoded TOTP secret. This parameter is mandatory.-Period: The time period in seconds for the TOTP code. The default is30seconds.-Digits: The number of digits for the TOTP code. The default is6digits.-Algorithm: The hashing algorithm to use for generating the HMAC hash. Supported algorithms are"SHA1","SHA256", and"SHA512". The default is"SHA1".
# Generate a 6-digit TOTP code using the provided secret, a 30-second period, and the SHA1 algorithm
$TOTPSecret = "JBSWY3DPEHPK3PXP"
$MFA = Convert-TOTPToMFA -Secret $TOTPSecret -Period 30 -Digits 6 -Algorithm "SHA1"
Write-Host "Generated TOTP Code: $MFA"Generated TOTP Code: 123456
- If the generated MFA code is not valid, ensure your system clock is synchronized with an NTP server.
- Check synchronization status:
w32tm /query /status - Start the Windows Time service if not running:
Start-Service w32time - Resynchronize the system clock:
w32tm /resync - Verify synchronization status again:
w32tm /query /status
- Check synchronization status:
- This function is based on the TOTP algorithm described in RFC 6238.