Post

NtfyPwsh - A PowerShell Module to Send Ntfy Messages

NtfyPwsh - A PowerShell Module to Send Ntfy Messages

๐Ÿ“ฑ NtfyPwsh - A PowerShell Module to Send Ntfy Messages

๐Ÿš€ Simplifying Push Notifications with NtfyPwsh: A PowerShell Module for Ntfy.sh

PowerShell Gallery Version PowerShell Gallery Downloads GitHub license

๐Ÿ“– Introduction

Ntfy is an amazing tool for push notifications. Itโ€™s simple, efficient, and can be self-hosted or used for free (as of this post) at https://ntfy.sh. As a lightweight pub-sub notification service, Ntfy allows you to send notifications to your phone or desktop from any script, application, or service.

To make interacting with Ntfy even easier for PowerShell users, Iโ€™ve developed a PowerShell module called NtfyPwsh that provides a simple interface for the Ntfy API.

๐Ÿ“ฅ Installing NtfyPwsh

NtfyPwsh can be installed from the PowerShell Gallery with a simple command:

1
Install-Module -Name NtfyPwsh -Scope CurrentUser

โš™๏ธ Requirements

Before you get started, make sure you have:

  • ๐Ÿ”น PowerShell 7 or higher
  • ๐Ÿ”น Access to the internet or your self-hosted Ntfy server
  • ๐Ÿ”น Windows or Linux (-Delay and -Email are not working under linux)

๐Ÿ› ๏ธ Core Functions

NtfyPwsh offers two primary functions:

  1. ๐Ÿ“ค Send-NtfyMessage: For sending notifications
  2. ๐Ÿ”˜ Build-NtfyAction: For creating interactive action buttons in notifications

Letโ€™s explore these functions and how they can be used.

๐ŸŒ Ntfy Server

When using NtfyPwsh, you can specify which Ntfy server to use:

  • If you donโ€™t specify a -URI parameter, NtfyPwsh will default to using https://ntfy.sh.
  • If youโ€™re using a self-hosted instance, simply include your server URL with the -URI parameter:
1
2
3
4
5
6
7
$ntfy = @{
    URI   = 'https://ntfy.yourdomain.com'  # Your self-hosted instance
    Topic = 'ntfypwsh'
    Title = 'Custom Server Notification'
    Body  = 'This message is sent to your private Ntfy server'
}
Send-NtfyMessage @ntfy

This flexibility allows you to use either the public Ntfy service or your own private instance without changing your code structure.

๐Ÿ“จ Sending Basic Notifications

The simplest way to send a notification is with a topic, title, and body:

1
2
3
4
5
6
$ntfy = @{
    Topic = 'ntfypwsh'
    Body  = 'This is a test message'
    Title = 'Test Title'
}
Send-NtfyMessage @ntfy

This sends a notification to the public โ€œntfypwshโ€ on ntfy.sh. Anyone subscribed to this topic will receive the notification.

๐Ÿ” Adding Authentication

If youโ€™re using a self-hosted Ntfy server or need to authenticate with the public server, NtfyPwsh supports two authentication methods (as of v0.4.0):

Note: As of v0.4.0, authentication uses -TokenCreds (API token via Get-Credential) or -Credential (username/password via Get-Credential). The -TokenPlainText parameter has been removed.

Ntfy supports both Basic (username/password) and Bearer (token) authentication. In PowerShell, you can use a PSCredential object for both methods:

  • Username/Password (Basic Auth): Use your username and password with Get-Credential or create a PSCredential manually.
  • API Token (Bearer or Basic): Use Get-Credential -UserName 'token' and enter your API token as the password, or create a PSCredential with username โ€˜tokenโ€™ and the token as the password.

The module will use the correct Authorization header for you.

๐Ÿ”‘ Using API Token (Bearer/Basic authentication)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Prompt for API token (recommended for interactive use)
$Creds = Get-Credential -UserName 'token' -Message 'Enter API Token as password'
$ntfy = @{
    URI   = 'https://ntfy.mydomain.com'
    Topic = 'ntfypwsh'
    Body  = 'This is a test message'
    Title = 'Test Title'
    TokenCreds = $Creds
}
Send-NtfyMessage @ntfy

# Or create a PSCredential manually (for scripting)
$Token = ConvertTo-SecureString 'tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2' -AsPlainText -Force
$Creds = [PSCredential]::new('token', $Token)
$ntfy = @{
    URI   = 'https://ntfy.mydomain.com'
    Topic = 'ntfypwsh'
    Body  = 'This is a test message'
    Title = 'Test Title'
    TokenCreds = $Creds
}
Send-NtfyMessage @ntfy

๐Ÿ”‘ Using Username/Password (Basic authentication)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Prompt for username and password
$Creds = Get-Credential -UserName 'admin' -Message 'Enter user password'
$ntfy = @{
    URI   = 'https://ntfy.mydomain.com'
    Topic = 'ntfypwsh'
    Body  = 'This is a test message'
    Title = 'Test Title'
    Credential = $Creds
}
Send-NtfyMessage @ntfy

# Or create a PSCredential manually (for scripting)
$Password = ConvertTo-SecureString 'fakepassword' -AsPlainText -Force
$Creds = [PSCredential]::new('testuser', $Password)
$ntfy = @{
    URI   = 'https://ntfy.mydomain.com'
    Topic = 'ntfypwsh'
    Body  = 'This is a test message'
    Title = 'Test Title'
    Credential = $Creds
}
Send-NtfyMessage @ntfy

โ„น๏ธ The module automatically generates the correct Authorization header for you, whether you use username/password or an API token.

๐Ÿ”˜ Creating Interactive Notifications with Actions

One of the most powerful features of Ntfy is the ability to add interactive action buttons to notifications. NtfyPwsh makes this easy with the Build-NtfyAction function:

1
2
3
4
5
6
7
8
9
10
11
$ntfy = @{
    URI   = 'https://ntfy.sh'
    Topic = 'ntfypwsh'
    Body  = 'This is a test message with actions'
    Title = 'Test Notification'
    Action = @(
        (Build-NtfyAction -ActionView -Label 'View Dashboard' -URL 'https://dashboard.example.com')
        (Build-NtfyAction -ActionHttp -Label 'Acknowledge' -URL 'https://api.example.com/acknowledge' -Method POST -Body '{"status":"acknowledged"}')
    )
}
Send-NtfyMessage @ntfy

You can include up to three actions per notification. The supported action types are:

  • ๐ŸŒ -ActionView: Opens a URL when clicked (requires -Label and -URL)
  • ๐Ÿ”„ -ActionHttp: Makes an HTTP request when clicked (requires -Label and -URL, supports -Method, -Headers, -Body)
  • ๐Ÿ“ฑ -ActionBroadcast: Broadcasts an Android intent (for Android only, requires -Label, supports -Intent, -Extras)

Note: As of 0.4.0 the -Action parameter in Build-NtfyAction has been replaced with explicit switches: -ActionView, -ActionHttp, or -ActionBroadcast. Update your scripts accordingly.

Example: Broadcast Action

1
2
$action = Build-NtfyAction -ActionBroadcast -Label 'Take Screenshot' -Extras @{ cmd = 'screenshot' }
Send-NtfyMessage -Topic 'ntfypwsh' -Body 'Please take a screenshot.' -Action $action

๐Ÿ“Ž Sending Notifications with Attachments

Need to include an image or file with your notification? NtfyPwsh supports attachments:

1
2
3
4
5
6
7
8
9
$ntfy = @{
    URI            = 'https://ntfy.sh'
    Topic          = 'ntfypwsh'
    Body           = 'Check out this screenshot'
    Title          = 'System Alert'
    AttachmentPath = 'C:/path/to/screenshot.png'
    AttachmentName = 'screenshot.png'
}
Send-NtfyMessage @ntfy

๐Ÿ’ผ Real-World Use Cases

๐Ÿ–ฅ๏ธ System Monitoring

1
2
3
4
5
6
7
8
9
10
11
12
# Check disk space and send alert if below threshold
$diskC = Get-PSDrive C
if ($diskC.Free / $diskC.Used -lt 0.1) {
    $ntfy = @{
        Topic    = 'ntfypwsh'
        Title    = 'โš ๏ธ Low Disk Space Alert'
        Body     = "Drive C: has only $([Math]::Round($diskC.Free / 1GB, 2)) GB free"
        Priority = 'High'
        Tags     = 'warning'
    }
    Send-NtfyMessage @ntfy
}

๐Ÿš€ Deployment Notifications

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Notify team when deployment completes
try {
    # Deployment code here
    $ntfy = @{
        Topic = 'ntfypwsh'
        Title = 'โœ… Production Deployment Successful'
        Body  = "v1.2.3 deployed to production at $(Get-Date)"
        Tags  = 'check'
        Action = Build-NtfyAction -ActionView -Label 'View Dashboard' -URL 'https://dashboard.example.com'
    }
    Send-NtfyMessage @ntfy
} catch {
    $ntfy = @{
        Topic    = 'ntfypwsh'
        Title    = 'โŒ Production Deployment Failed'
        Body     = "Error: $($_.Exception.Message)"
        Priority = 'High'
        Tags     = 'x'
    }
    Send-NtfyMessage @ntfy
}

๐Ÿ“… Schedule Notifications

1
2
3
4
5
6
7
8
9
# Schedule a meeting reminder with action to join
$ntfy = @{
    Topic  = 'ntfypwsh'
    Title  = '๐Ÿ“… Team Meeting'
    Body   = 'Weekly team meeting starts in 15 minutes'
    Delay  = '15m'
    Action = Build-NtfyAction -ActionView -Label 'Join Meeting' -URL 'https://meet.example.com/team'
}
Send-NtfyMessage @ntfy

๐Ÿ“Š Notification Priority Levels

Ntfy supports different priority levels for your notifications, which NtfyPwsh makes easy to set:

PriorityDescriptionUse Case
MaxUrgent notification, highest importanceCritical system failures
HighImportant notificationSecurity alerts
DefaultRegular notificationNormal updates
LowLow priority notificationInformational messages
MinMinimal priority, least disruptiveDebug information

Example:

1
2
3
4
5
6
7
8
$ntfy = @{
    Topic    = 'ntfypwsh'
    Title    = '๐Ÿ”ฅ Server Outage'
    Body     = 'Production server is not responding'
    Priority = 'Max'
    Tags     = 'rotating_light'
}
Send-NtfyMessage @ntfy

๐Ÿท๏ธ Using Tags in Notifications

Ntfy supports various emoji tags that you can add to your notifications. NtfyPwsh makes it easy to include these tags:

1
2
3
4
5
6
7
$ntfy = @{
    Topic = 'ntfypwsh'
    Title = 'System Update'
    Body  = 'System update completed successfully'
    Tags  = 'arrow_up,partying_face'
}
Send-NtfyMessage @ntfy

For a complete list of supported tags, check the Ntfy documentation.

๐Ÿ“– More Examples: NtfyPwsh TMI Edition

Looking for even more detailed usage scenarios, advanced parameter combinations, and real-world scripts? Check out the companion post:

โžก๏ธ NtfyPwsh TMI Edition: Full Examples โ€” A comprehensive guide with full examples and explanations for every parameter and advanced scenario in the NtfyPwsh module.

๐Ÿ”„ Conclusion

The NtfyPwsh PowerShell module simplifies the process of integrating push notifications into your PowerShell scripts. Whether youโ€™re monitoring systems, automating workflows, or just need a simple way to send yourself reminders, NtfyPwsh combined with the Ntfy service provides a powerful notification solution.

Give it a try and let me know what you think! Contributions and feedback are always welcome on the GitHub repository.

๐Ÿ“š Resources


GitHub Stars PowerShell Gallery

Licensed under the MIT License. Created by ptmorris1.

This post is licensed under CC BY 4.0 by the author.