r/PowerShell • u/Hour-Bat7014 • Jan 29 '26
Question Doesn't work from the command line
$InputFile = "E:\turnOffCharge.txt"
$OutputFile = "E:\sendOFF.txt"
$TodayDate = Get-Date -Format "yyyy-M-dd"
We read the file, filter the lines containing today's date, and save it.Get-Content -Path $InputFile | Where-Object { $_ -like "*$TodayDate*" } | Set-Content -Path $OutputFile
0
Upvotes
7
u/PJFrye Jan 29 '26
You say “command line”. Are you sure you are in powershell?
5
u/ByronScottJones Jan 29 '26
Please tell us in plain English what you're trying to achieve with this script. Then, tell us the error you're receiving.
3
u/jsiii2010 Jan 29 '26 edited Jan 29 '26
Works for me.
2
u/OlivTheFrog Jan 29 '26
Hi u/Hour-Bat7014
What error are you getting ? For me, this code works.
Are you sure you have a CRLF at the end of each line ?
regards
1
u/UserProv_Minotaur Jan 29 '26
You're probably better off running these in ISE or a Powershell session (powershell.exe) than from the command line (cmd.exe)
1
u/Anaconda077 Jan 29 '26
Maybe wrong date format? Should be "yyyy-MM-dd" instead of "yyyy-M-dd"?
-2
u/Cholsonic Jan 29 '26
If you are trying to match all the lines that contain today's date string, then you are going to have to convert that date object into a string first.