Here’s a fun PowerShell script that you can use to create a simple multi-channel chat room. All you need is a network share where everyone has read and write permissions.
The chat is file-based and makes use of PowerShell’s ability to monitor files for changes. So essentially, each chat channel is a text file, and whenever someone wants to “say” something, a text line is added to the file. Anyone connected to the chat actually monitors this file for changes.
Obviously, this “chat” is just for experimenting and has a number of limitations. For example, when someone writes to the file, no one else can write to it. However, it nicely illustrates how PowerShell can monitor files and take action when new text is added. You can take this technique and apply it to log files as well: PowerShell can notify you when log files are appended, and even auto-filter the newly added text to send out warnings or take action on trigger words.
Before you start, make sure you adjust $ServerShare and set it to a read/write network share.
Next, you can enter the chat like so:
Enter-Chat -ChatChannelName lunchbreak -Name Tobias -ShowOldPosts
-ShowOldPosts echos the existing chat messages. Without this, you see only new messages. Whenever you run Enter-Chat, it checks for a file with the name specified in -ChatChannelName, and if it is still missing, the file is created.
Get-ChatChannel lists all chat files found on the share, plus the time the channel was last used. This information is taken entirely from the file properties (LastWriteTime).
# make sure you adjust this path
# it must point to a network share where you have read and write permissions
$ServerShare = "\\myserver\chathome"
$Name = $env:USERNAME,
$HomeShare = $ServerShare
$Option = ''
$Option = '-Tail 0'
$Path = Join-Path -Path $HomeShare -ChildPath "$ChatChannelName.txt"
$exists = Test-Path -Path $Path
if ($exists -eq $false)
$null = New-Item -Path $Path -Force -ItemType File
$process = Start-Process -FilePath powershell -ArgumentList "-noprofile -windowstyle hidden -command Get-COntent -Path '$Path' $Option -Wait | Out-GridView -Title 'Chat: [$ChatChannelName]'" -PassThru
Write-Host "To exit, enter: quit"
"[$Name entered the chat]" | Add-Content -Path $Path
Write-Host "[$ChatChannelName]: " -ForegroundColor Green -NoNewline
$inputText = Read-Host
$isStopCommand = 'quit','exit','stop','leave' -contains $inputText
if ($isStopCommand -eq $false)
"[$Name] $inputText" | Add-Content -Path $Path
} until ($isStopCommand -eq $true)
"[$Name left the chat]" | Add-Content -Path $Path
$process | Stop-Process
$HomeShare = $ServerShare
Get-ChildItem -Path $HomeShare -Filter *.txt -File |
ChannelName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
LastActive = $_.LastWriteTime
Started = $_.CreationTime
ReTweet this Tip!
Powered by IDERA