PSUtil

repository·master·Indexed 41 days ago

https://github.com/powershellframeworkcollective/psutil

A convenience module for PowerShell designed to increase terminal efficiency through short aliases, pipeline-friendly functions, and keybindings. It includes utilities like grep (alias for Select-String), exp (wrapper for Expand-PSUObject), and functions for quick navigation to the desktop and File Explorer. PSUtil leverages PSFramework for configuration management and TEPP for custom tab completion.

Tokens
1.9K
Snippets
7
Records
16
Agent score
51%

What's inside PSUtil

  1. Handle cross-platform paths for Linux and MacOS

    master

    When developing modules intended to run on Linux and MacOS, remember that these operating systems are case-sensitive regarding paths and files.

    To ensure path compatibility:

    • Use Import-ModuleFile: This command is preconfigured to resolve file paths, reliably converting notations that might otherwise fail on different systems. Content imported via this command does not need to worry about specific path separators.
    • Use Path Resolution Utilities: To make your own code resilient to OS-specific path notations, use Resolve-Path or the more robust Resolve-PSFPath instead of hardcoding string paths.
  2. Implement custom tab completion with TEPP

    master

    Tab Expansion Plus Plus (TEPP) allows you to define custom options that users are offered when tabbing through input parameters in PowerShell. This is achieved by defining a scriptblock that returns a list of strings and then assigning that scriptblock to a command parameter.

    To implement custom tab completion, follow these two steps:

    1. Define the completion logic: Create a scriptblock that executes when the user hits TAB and returns the available string options.
    2. Assign the completion logic: Assign that scriptblock to the specific parameter of your command. You can reuse the same scriptblock for multiple parameters.
  3. Use the bin folder for binary data and type accelerators

    master

    The bin folder is used to store binary data and scripts related to the PSUtil type system. You can use this directory to manage:

    • Your own C#-based libraries.
    • Third-party libraries (ensure you comply with their licenses).
    • Scripts that declare Type Accelerators (which act as aliases for .NET types).

    For managing type accelerators, refer to the Set-PSFTypeAlias help documentation.

  4. Configure settings using PSFramework

    master

    PSUtil leverages the PSFramework configuration system to manage settings. This system provides a robust way to handle application parameters with the following capabilities:

    • Publishing settings: Make settings available for discovery.
    • Onboard documentation: Include documentation directly with the settings.
    • Input validation: Ensure settings meet specific criteria before application.
    • Reactive scripts: Execute specific scripts automatically when a setting changes.
    • User discovery: Allow users to discover and update settings easily.
    • Policy administration: Manage settings via Group Policy or Desired State Configuration (DSC).

    For detailed implementation guidance and complex configuration scenarios, visit the official documentation at http://psframework.org.

  5. Organize TEPP script files and assignments

    master

    When using the TEPP scaffold, the order of imports is critical for functionality. Organize your files according to these naming conventions and roles:

    • Definition Files: All script files that define the completion scriptblocks must use the *.tepp.ps1 extension.
    • Assignment Files: All logic that assigns these scriptblocks to command parameters should be placed in assignment.ps1.

    Ensure that the definition files are loaded before the assignment files to prevent errors during the assignment phase.

  6. Structure a module for PSFramework compatibility

    master

    To implement the PSFramework-optimized module layout, organize your files and manifest as follows:

    1. Public Functions: Place all functions intended for end-users in the /functions/ directory.
    2. Internal Functions: Place helper functions that should remain hidden from the user in the /internal/functions/ directory.
    3. Module Manifest: Update the FunctionsToExport node in your module manifest (PSUtil.psd1) with a list of all functions you wish to publish.
    # Example PSUtil.psd1 update
    FunctionsToExport = @('Get-PublicFunction', 'Set-PublicFunction')
  7. Ensure PSUtil keybindings are always available

    master

    While PowerShell automatically imports modules when a function is called, keybindings are not registered until the module is explicitly imported. To ensure all aliases and keybindings are available immediately upon starting a session, add Import-Module PSUtil to your PowerShell profile.

    You can edit your profile by running:

    notepad $profile
  8. Use the exp function to expand objects

    master

    The exp function (an alias/wrapper for Expand-PSUObject) simplifies the use of Select -Expand. You can use it to expand a specific property or use it without arguments to expand the current object in the pipeline.

    # Expand a specific property
    dir | exp FullName
    
    # Expand the current object
    dir | exp
    
    # Combined with grep
    "abc def ghi" | grep "(d\w+)" | exp
  9. Open folders in File Explorer with Invoke-PSUExplorer

    master

    The explorer function (an alias for Invoke-PSUExplorer) allows you to open the current directory, a directory from the pipeline, or a specific module's path in Windows Explorer.

    # Open current folder
    explorer
    
    # Open a folder from the pipeline
    dir | explorer
    
    # Open a specific module's folder
    gmo PSUtil | explorer