Title: Add `$Now` to the automatic variables · Issue #24079 · PowerShell/PowerShell · GitHub
Open Graph Title: Add `$Now` to the automatic variables · Issue #24079 · PowerShell/PowerShell
X Title: Add `$Now` to the automatic variables · Issue #24079 · PowerShell/PowerShell
Description: Summary of the new feature / enhancement PowerShell's expression - and argument parsing modes generally results in a need syntax but could also overwhelm your script with Parentheses, Braces and Square Brackets. Wherever I can lose some ...
Open Graph Description: Summary of the new feature / enhancement PowerShell's expression - and argument parsing modes generally results in a need syntax but could also overwhelm your script with Parentheses, Braces and Sq...
X Description: Summary of the new feature / enhancement PowerShell's expression - and argument parsing modes generally results in a need syntax but could also overwhelm your script with Parentheses, Braces an...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/24079
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `$Now` to the automatic variables","articleBody":"### Summary of the new feature / enhancement\r\n\r\nPowerShell's expression - and argument parsing modes generally results in a need syntax but could also overwhelm your script with Parentheses, Braces and Square Brackets. Wherever I can lose some of them, it would make my script more readable...\r\n\r\n`Get-Date` is a cmdlet that pushes the parser in **argument mode** where it often is used as a **current time variable** in an **expression**.\r\nMeaning it requires to use [Parentheses (`()`) to begin a new expression](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_parsing#argument-mode):\r\n\r\n```PowerShell\r\n$Start = Get-Date\r\n# Do some things\r\nWrite-Debug \"Elapsed time: $((Get-Date) - $Start)\"\r\n```\r\n\r\nOr a [subexpression (`$()`)](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_operators#subexpression-operator--) when used e.g. in a string:\r\n\r\n```\r\nif ([datetime]::IsLeapYear($(Get-Date).Year)) { Write-Host \"$((Get-Date).Year) is a leap year\" }\r\n```\r\n\r\n### Proposed technical implementation details (optional)\r\n\r\nSpecifically for the getting the current `DateTime` additional parenthesis could be avoided if there would exist a `$Now` in the list of [automatic variables](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables).\r\nWishful thinking:\r\n\r\n```PowerShell\r\n$Start = $Now\r\n# Do some things\r\nWrite-Debug \"Elapsed time: $($Now - $Start)\"\r\n```\r\n\r\n```\r\nif ([datetime]::IsLeapYear($Now.Year)) { Write-Host \"$($Now.Year) is a leap year\" }\r\n```\r\n\r\n---\r\n**Update** based on @kilasuit's referral and other comments below:\r\n\r\n> I was under the impression that automatic variables could be simply overruled but this is apparently wrong.\r\n\r\nMaybe the term \"*automatic variable*\" doesn't apply here as usually automatic variables can't be overwritten but if there would exists a common and standard `$Now` as described [here](https://stackoverflow.com/a/12043289/1701026):\r\n\r\n```PowerShell\r\n$Now = Get-Date\r\n$null = Set-PSBreakpoint -Variable Now -Mode Read -Action { \r\n Set-Variable -Scope 1 -Name Now -Value (Get-Date) \r\n}\r\n```\r\n\r\nI might do things along with:\r\n```PowerShell\r\nif ([datetime]::IsLeapYear($Now.Year)) { Write-Host \"$($Now.Year) is a leap year\" }\r\n2024 is a leap year\r\n```\r\n\r\n**caveats**\r\n* The variable should be overwritable (similar as putting the definition in my profile) to prevent any break changes.\r\n * I might of cause put this `$Now` definition in my own profile but that would not make it common (meaning that I have to redefine it in my shared scripts).","author":{"url":"https://github.com/iRon7","@type":"Person","name":"iRon7"},"datePublished":"2024-07-24T10:38:41Z","url":"https://github.com/PowerShell/PowerShell/issues/24079"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:27854881-634b-bc16-9d8f-77a6fb759e17 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E53E:2FD62:9082B:BE9CA:6AFD2AAB |
| html-safe-nonce | 756bc2956b12ceaf02b7e80424711c558be307a8764436179226e14f44d604a3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTNFOjJGRDYyOjkwODJCOkJFOUNBOjZBRkQyQUFCIiwidmlzaXRvcl9pZCI6Ijc1NTg2MzcwNzU0MjgwOTI1ODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | df7cf13364cb56626297c0ccf287e02f23a8bfa717c9b6a74386ff8dff8c085c |
| hovercard-subject-tag | issue:2427219803 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/24079/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8b38f953e123059cca3a200eefff4371a96871b7bd5145c135343529a56e6f6e/PowerShell/PowerShell/issues/24079 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8b38f953e123059cca3a200eefff4371a96871b7bd5145c135343529a56e6f6e/PowerShell/PowerShell/issues/24079 |
| og:image:alt | Summary of the new feature / enhancement PowerShell's expression - and argument parsing modes generally results in a need syntax but could also overwhelm your script with Parentheses, Braces and Sq... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iRon7 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 065128366c233a9d57ff68ab6c96e25b99dd1b0ab70d23b70873d3dd6b5e4f19 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 941021edf0c2ec58298f4a2c8f27ae507d129c92 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width