PowerShell 7
13 TopicsCannot dot-source this command because it was defined in a different language mode.
Microsoft.PowerShell_profile.ps1: Cannot dot-source this command because it was defined in a different language mode. To invoke this command without importing its contents, omit the '.' operator. I am getting this error when I open my PowerShell anywhere... I have few scripts present in my PS Profile, but this is failing with error, and I can't import, its blocking me to even set a property. $Host.Name = 'Test' InvalidOperation: Cannot set property. Property setting is supported only on core types in this language mode. Can someone please help me to get rid of this issue. Thanks!41KViews0likes12CommentsHuge memory consumption using ForEach-Object -Parallel
I’ve a function that updates the stats in databases using ForEach-Object -Parallel. When I run it, to loop through the stats in a database and update them, it consumes a lot of memory on the server where I run the command from. In some cases 60/70Gb. Why is it doing that? Is there a way I can have the function release the memory used for each stat it updates? Function Update-Stats { <# $Database = '' $SqlInstance = '' Update-Stats -SqlInstance $SqlInstance -Database $Database #> [cmdletbinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$SqlInstance , [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Database ) BEGIN { $FlagOn = "DBCC TRACEON (7471, -1) WITH NO_INFOMSGS" $FlagOff = "DBCC TRACEOFF (7471, -1) WITH NO_INFOMSGS" $Satslist = @() $Srv = Get-SqlInstance -ServerInstance $SqlInstance $Srv | Get-SqlDatabase -Name $Database | %{ $Db = $_ "Processing $($Db.Name)" $Db.Tables | ?{$_.RowCount -gt 10} | % { $Table = $_ $Table.Statistics | %{ $Satslist += [PSCUstomObject]@{ SqlInstance = $SqlInstance Database = $Database Schema = $Table.Schema Table = $Table.Name RowCount = $Table.RowCount Stats = $_.Name } } } } $Satslist } PROCESS { if($Srv.VersionMajor -ge 14 ) { Invoke-Sqlcmd -ServerInstance $SqlInstance -Database $Database -Query "$FlagOn" $Tflag = $true } $Satslist | Sort-Object {Get-Random} | % -parallel { $Obj = $_ $Table = "[$($Obj.Schema)].[$($Obj.Table)]" $Stats = "[$($Obj.Stats)]" switch($($Obj.RowCount)){ {$_ -le 1000000} {$Percent = 20 ; break} {$_ -In 1000001..10000000} {$Percent = 15 ; break} {$_ -In 10000001..100000000} {$Percent = 10 ; break} {$_ -ge 100000001} {$Percent = 5 ; break} } "Invoke-Sqlcmd -ServerInstance $($Obj.SqlInstance) -Database $($Obj.Database) -Query `"Update Statistics $Table $Stats with Sample $Percent Percent`"" Invoke-Sqlcmd -ServerInstance $($Obj.SqlInstance) -Database $($Obj.Database) -Query "Update Statistics $Table $Stats with Sample $Percent Percent" } -ThrottleLimit 12 if($Tflag) { Invoke-Sqlcmd -ServerInstance $SqlInstance -Database $Database -Query "$FlagOff" } } END { } }3.6KViews0likes6CommentsJoin computer to domain with PowerShell 7
PowerShell 7 does not support the Add-Computer cmdlet and does not support WMI. I am assuming one needs to use CIM now to add a computer to a domain with PowerShell. Is that correct, or is there a new cmdlet we should be using that replaced Add-Computer? If CIM is required, does any have syntax used for doing this that works in both WPS 5.1 and PS 7.x? Would it involve using Invoke-CimMethod? Thanks NKSolved3.3KViews0likes1CommentPowerShell Colors for Operator & Parameter
Hi all I have some weird behavior here. When I work with PowerShell 7 over the Windows Console Host, the Operator and Parameter color are completely black, and I can't see them when I type an Operator or Parameter. When I work with PS7 over the Windows Terminal, the Operator and Parameter are visible. Both windows have the same background color (0c0c0c) and I copied the value from the Windows Console Host PS7, to make sure it has the same value as the window from the Windows Terminal PS7. Has anyone a fix/explanation for this? Thanks. 🙂2.9KViews1like8CommentsUsing Desired State Configuration (DSC) v3 on Windows Server 2025
Microsoft Desired State Configuration v3 has some substantive changes compared to previous versions of the technology. Unlike PowerShell Desired State Configuration (PSDSC) v1.1 and v2, DSC v3 operates as a standalone command rather than a service, eliminating the need for a local configuration manager. This architectural change makes DSC v3 easier to use and scale, allowing any tool that can execute commands on Windows Server, such as Scheduled Tasks, to apply DSC configurations. You can install DSC using WinGet. Windows Server 2025 includes built-in support for WinGet, making the installation process more straightforward than on previous server versions. However, it's important to note that winget is only available for Windows Server 2025 with Desktop Experience and can’t be used with Server Core deployments. DSC v3 requires PowerShell 7.2 or later. As Windows Server 2025 comes with Windows PowerShell 5.1, you'll need to install PowerShell 7.x separately, which you can do manually or using WinGet from an elevated PowerShell session. winget install microsoft.powershell The PSDesiredStateConfiguration module specifies a minimum PowerShell version of 7.2 in its module manifest. WinGet provides the most straightforward installation experience with automatic updates: Open PowerShell with administrative privileges Install the stable version using: winget install Microsoft.DSC Alternatively, download the latest release from the PowerShell/DSC repository and add the folder containing the expanded archive contents to your PATH environment variable. Basic DSC v3 Commands To view all available commands in DSC v3, use: dsc --help The output will display available commands including: completer - Generate a shell completion script config - Apply a configuration document resource - Invoke a specific DSC resource schema - Get the JSON schema for a DSC type help - Display help information for commands To see the resources available for use in configurations: dsc resource list This command displays all DSC resources installed on your system that can be used in configuration documents. Creating DSC v3 Configurations DSC v3 configurations can be written in YAML format, which is more concise and readable than the MOF format used in previous versions. A DSC v3 configuration document must include: A reference to the DSC resource schema At least one resource definition with properties Creating a Basic Configuration Here's an example of a basic configuration in YAML format: $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json resources: - name: Force icon view in Control Panel type: Microsoft.Windows/Registry properties: keyPath: HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer\ valueName: ForceClassicControlPanel valueData: DWord: 1 This configuration sets a registry value to force the Control Panel to display using the classic icon view. For compatibility with classic PowerShell resources, you can use the WindowsPowerShell resource type: $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json metadata: name: IIS-Configuration resources: - name: Use Windows PowerShell resources type: Microsoft.Windows/WindowsPowerShell properties: resources: - name: Web server install type: PSDesiredStateConfiguration/WindowsFeature properties: Name: Web-Server Ensure: Present This configuration uses the classic WindowsFeature resource from the PSDesiredStateConfiguration module to install IIS. Once you've created a configuration document, you can test and apply it using DSC v3 commands. To test a configuration to see what changes would be made (rather than shouting Cowabunga on production systems) run the following command: dsc config get -f .\iis-config.yaml This command assesses the current state without making changes, showing what would happen if you applied the configuration. To apply a configuration run a version of the command (with your yaml file specified): dsc config set -f .\iis-config.yaml This command applies the configuration defined in the YAML file to your system. You can find out more about DSC v3 at the following locations: https://devblogs.microsoft.com/powershell/get-started-with-dsc-v3/ https://github.com/PowerShell/PSDesiredStateConfiguration https://learn.microsoft.com/powershell/dsc/overview?view=dsc-3.0&preserveView=true https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.52.1KViews4likes1CommentRegarding the different output formats of Powershell Versions 5 and 7.
I use Exchange Online commands through Powershell. I have noticed that Powershell versions 5 and 7 work differently. And I would like to achieve an output format like Powershell version 5, but without using the "|Format-table" notation. Is it possible to rewrite the default values? Thank you. Powershell version information in use: PowerShell 5: 5.1.22000.653 and PowerShell 7: 7.2.5Solved1.1KViews0likes3CommentsPowerShell support on Linux
I am trying to invoke Powershell 7.4 on Linux RHEL8 through a java program and submitting commands and reading output using streams. As soon as the powershell starts , the inputstream is reading some unknown characters from the powershell output for e.g. '[?1h' and '[?1l'. I need a way such that I don't read these characters. I tried setting the encoding in the input stream to UTF-8, but that didn't solve the problem. Would really appreciate if someone could provide some inputs on this.554Views0likes2Comments