r/PowerShell 13h ago

Script Sharing I built MonitorAutoSwitch - automatically moves my laptop display to the correct side of my monitor (home vs office) based on my public IP

7 Upvotes

The problem: at home my laptop sits to the right of my ultrawide, at the office it sits to the left of the monitor. Windows kept getting confused after docking, so every arrival was alt-tabbing into Display Settings and dragging rectangles around.

What it does: a small PowerShell tool that detects where I am via my public IP and repositions the laptop display on the correct side of the main monitor. Triggers on logon, wake from sleep and display connect, plus a once-a-minute display-count watcher. Comes with a WinForms setup wizard, a system tray toggle, and a per-user installer (no admin rights, everything lives in %LOCALAPPDATA% and Task Scheduler).

The bug that took months: the first version stored absolute coordinates (X=5120) and randomly failed with DISP_CHANGE_FAILED (-1) from ChangeDisplaySettingsEx. Root cause turned out to be two-fold: the monitor sometimes runs at a non-native resolution, and a non-DPI-aware PowerShell process sees scaled coordinates - so the target position landed outside the desktop. The fix: store only "left" or "right" + a Y offset, compute the real X from the live display widths on every run, and opt the process into per-monitor DPI awareness via SetProcessDpiAwarenessContext. Also worth knowing: Windows can hand out high device numbers like \.\DISPLAY13, so don't enumerate only DISPLAY1-9.

GitHub (MIT): https://github.com/dimitrihilverda/MonitorAutoSwitch

Full disclosure: built in pair-programming style with Claude - the design decisions and months of debugging pain are mine, a good chunk of the typing was not. Feedback on the Win32 interop is very welcome.


r/PowerShell 12h ago

Question Add Date Taken as prefix to filename

7 Upvotes

I need some help. I have lots of photos (IMG_xxxx.jpg), and I would like to change the filenames using this convention (yyyymmdd_IMG_xxxx.jpg). This is the command line I am using in PowerShell:

Get-ChildItem -File | Rename-Item -NewName { $_.LastWriteTime.ToString("yyyyMMdd_") + $_.Name }

This seemed to work great until I realized the last write time didn't match the date taken. I want to use the date taken from the EXIF field.

I have found the GetDetailsOf command and know that Date Taken is the 12th property. How can I use this in a command line prompt in PowerShell?

Assumptions for my specifics:

  • all files are .jpg with Date Taken data
  • all files are in the current folder
  • renamed files will replace original in the current folder

This way, I don't have to worry about writing a script to determine path names or renaming to a different folder.

The code provided above works perfectly if the date I wanted was the last write time. Is there an easy way to modify this to use Date Taken instead?

TIA


r/PowerShell 11h ago

Script Sharing What have you done with PowerShell this month?

4 Upvotes

A sticked post for the community to share their projects throughout the month.

Make sure to post a link to the code!