For years, I’ve been using the following PowerShell code in my profile file to make sure I’m using ISO 8601 format for date.
$culture = Get - Culture
$culture . DateTimeFormat . ShortDatePattern = 'yyyy-MM-dd'
Set - Culture $culture
I’ve just installed PowerShell 7 and suddenly this code does not work anymore. I got this error message:
2 | $culture.DateTimeFormat.ShortDatePattern = ‘yyyy-MM-dd’
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception setting “ShortDatePattern”: “Instance is read-only.”
So, what is the new and correct way of doing this in PowerShell 7? Please.
Olaf
April 16, 2020, 10:33am
2
When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.
https://social.technet.microsoft.com/Forums/en-US/99a81774-e634-4263-be84-23a572208a3f/how-to-set-correctly-date-short-format?forum=winserverpowershell
Thanks
This is interesting. Based on the closed issue linked below, it is being stated that it’s read only, always has been, not a bug, and working as designed.
opened 07:48AM - 11 Jan 19 UTC
closed 02:18PM - 11 Jan 19 UTC
Resolution-External
<!--
BEFORE SUBMITTING A NEW ISSUE, PLEASE READ THE TROUBLESHOOTING DOCS!
ht… tps://github.com/PowerShell/vscode-powershell/tree/master/docs/troubleshooting.md
IMPORTANT: you can generate a bug report directly from the
PowerShell extension in Visual Studio Code by selecting
"PowerShell: Upload Bug Report to GitHub" from the command palette.
The more repro details you can provide, along with a zip
of the log files from your session, the better the chances
are for a quick resolution.
You may also want to record a GIF of the bug occurring and
attach it here by dropping the file into the description body.
-->
### System Details
<!--
To help diagnose your issue, the following details are helpful:
- Operating system name and version
- VS Code version
- PowerShell extension version
- Output from `$PSVersionTable`
To get this information, run the following expression in your Integrated Console and paste the output here inside the backticks below:
& {"### VSCode version:1.26.1 493869ee8e8a846b0855873886fc79d480d342de x64"; "`n### VSCode extensions:`ndbaeumer.vscode-eslint@1.8.0
DotJoshJohnson.xml@2.4.0
eamodio.gitlens@9.4.1
eg2.tslint@1.0.42
formulahendry.code-runner@0.9.5
ms-mssql.mssql@1.4.0
ms-vscode.csharp@1.17.1
ms-vscode.PowerShell@1.10.2
redhat.java@0.36.0
vscjava.vscode-java-debug@0.16.0
vscjava.vscode-java-dependency@0.3.0
vscjava.vscode-java-pack@0.5.0
vscjava.vscode-java-test@0.13.0
vscjava.vscode-maven@0.13.0
### PSES version: 1.10.2.0
### PowerShell version:
Name Value
---- -----
PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
If you are running VSCode Insiders, use this expression instead (and paste the result inside the backticks):
& {"### VSCode version: $(code-insiders -v)"; "`n### VSCode extensions:`n$(code-insiders --list-extensions --show-versions | Out-String)"; "`n### PSES version: $($pseditor.EditorServicesVersion)"; "`n### PowerShell version:`n$($PSVersionTable | Out-String)"}
-->
System Details Output
```
### VSCode version: 1.26.1 493869ee8e8a846b0855873886fc79d480d342de x64
### VSCode extensions:
dbaeumer.vscode-eslint@1.8.0
DotJoshJohnson.xml@2.4.0
eamodio.gitlens@9.4.1
eg2.tslint@1.0.42
formulahendry.code-runner@0.9.5
ms-mssql.mssql@1.4.0
ms-vscode.csharp@1.17.1
ms-vscode.PowerShell@1.10.2
redhat.java@0.36.0
vscjava.vscode-java-debug@0.16.0
vscjava.vscode-java-dependency@0.3.0
vscjava.vscode-java-pack@0.5.0
vscjava.vscode-java-test@0.13.0
vscjava.vscode-maven@0.13.0
### PSES version: 1.10.2.0
### PowerShell version:
Name Value
---- -----
PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
```
### Issue Description
I am experiencing a problem with setting the (get-culture).DateTimeFormat.ShortDatePattern
#### Expected Behaviour
I expected the statement below to run without errors
(get-culture).DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
When I execute this in a normal powershell window it changes the ShortDatePattern property to the new value whereas when I do this inside the powershell console in VS Code I get an error that the property is readonly.
#### Actual Behaviour
I get the error
Exception setting "ShortDatePattern": "Instance is read-only."
At line:1 char:1
+ (Get-Culture).DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
### Attached Logs
Follow the instructions in the [troubleshooting docs](https://github.com/PowerShell/vscode-powershell/blob/master/docs/troubleshooting.md#logs)
about capturing and sending logs.
However that is not true, in 5.1 you are able to set it the way you show.
<p style=“text-align: left;”>https://imgur.com/VXXYwar </p>
Now I thought I was onto something with this
https://imgur.com/g37C9BR
As you can see it didn’t error but it didn’t change either. I’d say there is a bug, either in Windows Powershell for it working, or in pwsh 6/7 for it not.I tested on 6.2.1