The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,115 other followers

Archive for the ‘PowerShell’ Category

PowerShell: checking minimum version

Posted by jpluimers on 2020/08/13

Nowadays, often your PowerShell code uses features unavailable in older PowerShell versions. When running it on a version that is too old, you usually get an error message, for instance like this:

Unable to find type [Ordered]: make sure that the assembly containing this type is loaded.

Back in the days, this was a new feature introduced in PowerShell 3.0: [WayBack] Use cases of [ordered], the new PowerShell 3.0 feature – Stack Overflow

It is way friendlier to show a message indicating the version is too old in stead of throwing this error.

That’s where the # Requires Version 3.0 directive comes in: [WayBackabout_Requires | Microsoft Docs.

Adding this line to the top of a script gives output like this on a stock Windows 7 SP1 system that has PowerShell 2.0:

# PowerShell -f List-Delphi-Installed-Packages.ps1
The script ‘List-Delphi-Installed-Packages.ps1’ cannot be run because it contained a “#requires” statement at line 1 for Windows PowerShell version 3.0. The version required by the script does not match the currently running version of Windows PowerShell version 2.0.
+ CategoryInfo : ResourceUnavailable: (List-Delphi-Installed-Packages.ps1:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion

Note that PowerShell 3.0 is also the minimum version for debugging it in Visual Studio Code (which means you do not have to use PowerShell ISE any more; it is still there , but so far behind as a development tool that many prefer Visual Studio Code):

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

Installing and updating Windows PowerShell – via Microsoft Docs

Posted by jpluimers on 2020/08/11

Since I keep forgetting that PowerShell is part of WMF (Windows Management Framework) and about the compatibility/installation matrix: [WayBack] Installing Windows PowerShell | Microsoft Docs:

The installation package for PowerShell comes inside a WMF installer. The version of the WMF installer matches the version of PowerShell; there’s no stand alone installer for Windows PowerShell.

If you need to update your existing version of PowerShell, in Windows, use the following table to locate the installer for the version of PowerShell you want to update to.

Windows PS 3.0 PS 4.0 PS 5.0 PS 5.1
Windows 10 (see Note1)
Windows Server 2016
installed
Windows 8.1
Windows Server 2012 R2
installed [WayBack] WMF 5.0 [WayBack] WMF 5.1
Windows 8
Windows Server 2012
installed [WayBack] WMF 4.0  [WayBack] WMF 5.0 [WayBack] WMF 5.1
Windows 7 SP1
Windows Server 2008 R2 SP1
[WayBack] WMF 3.0 [WayBack] WMF 4.0 [WayBack] WMF 5.0 [WayBack] WMF 5.1

To upgrade to WMF 5.0 from 4.0 you need to install .net 4.5 or later on your machine first. Then install WMF 5.0 RTM.

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

When NTFS shrink fails, despite using the default settings from the shrink dialog

Posted by jpluimers on 2020/05/25

Sometimes an NTFS shrink still fails, even though you use the built in Windows defragmentation tools, of SysInternals contig tool.

The best you can do is to follow the steps in:

  1. run diskmgmt.msc to try shrinking the disk, then often it is already in the error message: “You can’t shrink a volume beyond the point where any unremoveable files are located see the defrag event in application log for detailed information about the operation when it has completed.”
  2. use eventvwr.exe and look at the Windows Logs for the most recent Application entries that has Source set to defrag

Those defrag entries usually tell about the last file that could not be moved.

You can use wevtutill to query events on the commandline.

Note that contrary to [WayBack] WEVTUTIL – Windows CMD – SS64.com documentation, the option /rd cannot be expanded to /reversedirection , as you will get an error “invalid option reversedirection” – Google Search.

For querying the above defrag event, use this command line (replace /format:XML with /format:text for more readable but also more verbose output):

wevtutil query-events Application /count:2 /format:XML /rd:true /query:"*[System[(EventID=259)]]"

On Windows 10, this is often caused by “System Protection” which locks files under C:\Recovery, but I have also seen $BITMAP, $MFT and $DATA entries.

System protected drives

To view which drives are currently used for system protection (this opens the “System Properties” dialog focussed on the “System Protection” tab):

SystemPropertiesProtection.exe

To disable it for one drive:

Disable-ComputerRestore -Drive "C:"

To enable it for one drive:

Enable-ComputerRestore -Drive "C:"

There seems to be no easy one-command PowerShell way to view the drives have ComputerRestore enabled, as this does not show drive letters:

PowerShell Get-ComputerRestorePoint ^| Format-List

The above gives more detailed output than a plain PowerShell Get-ComputerRestorePoint

Deleting restore points

PowerShell does not have a built-in option to delete restore points, but vssadmin has, but calls them “shadows”.

First list them:

vssadmin list shadows

Then delete them (but be aware this will not prompt for confirmation because of the /quiet):

vssadmin delete shadows /for=C: /quiet

You can also delete them for all drives (this will not prompt for confirmation either):

vssadmin delete shadows /all /quiet

Stopping the volume shadow copy service:

net stop vss

Managing hibernation and page file

Hibernation:

powercfg.exe /hibernate off

powercfg.exe /hibernate on

Page file:

wmic pagefile list /format:list
AllocatedBaseSize=2944

CurrentUsage=0
Description=C:\pagefile.sys
InstallDate=20181018215808.683376+120
Name=C:\pagefile.sys
PeakUsage=0
Status=
TempPageFile=FALSE

wmic computersystem where name="%computername%" get AutomaticManagedPagefile
AutomaticManagedPagefile
TRUE

wmic computersystem where name="%computername%" set AutomaticManagedPagefile=False
Updating property(s) of '\\MYCOMPUTER\ROOT\CIMV2:Win32_ComputerSystem.Name="LAPTOPUW08"'
Property(s) update successful.

wmic computersystem where name="%computername%" get AutomaticManagedPagefile
AutomaticManagedPagefile
FALSE

wmic.exe pagefileset where name="C:\\pagefile.sys" delete
Deleting instance \\MYCOMPUTER\ROOT\CIMV2:Win32_PageFileSetting.Name="C:\\pagefile.sys"
Instance deletion successful.

Sometimes the deletion does not work (see below for workaround):

wmic pagefile list /format:list

AllocatedBaseSize=2944
CurrentUsage=0
Description=C:\pagefile.sys
InstallDate=20181018215808.683376+120
Name=C:\pagefile.sys
PeakUsage=0
Status=
TempPageFile=FALSE

Do not do this:

wmic pagefile delete
Deleting instance \\MYCOMPUTER\ROOT\CIMV2:Win32_PageFileUsage.Name="C:\\pagefile.sys"
ERROR:
Description = Provider is not capable of the attempted operation

wmic pagefileset set name="c:\\pagefile.sys",InitialSize=0,MaximumSize=0
No Instance(s) Available.

Sometimes it still fails, so then you have to use the UI:

  1. Run SystemPropertiesAdvanced.exe
  2. Under Performance, click on Settings
  3. Click the Advanced tab
  4. Under Virtual memory, click the Change button
  5. Ensure Automatically manage page file size for all drives is disabled
  6. Ensure No paging file is selected
  7. Click the Set button
  8. Confirm you really want no page file
  9. Press on the three OK buttons to fully leave the Advanced System Properties dialog.
  10. Reboot

After resizing the disk, reverse the steps:

  1. Run SystemPropertiesAdvanced.exe
  2. Under Performance, click on Settings
  3. Click the Advanced tab
  4. Under Virtual memory, click the Change button
  5. Ensure Automatically manage page file size for all drives is enabled
  6. Confirm you really want no page file
  7. Press on the three OK buttons to fully leave the Advanced System Properties dialog.
  8. Reboot

Bitmap file

Sometimes the file blocking the resize is the NTFS "\$BitMap::$DATA", which few defragmentation tools can move as it is the MFT Bitmap.

Background reading

–jeroen

Posted in CommandLine, Console (command prompt window), Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »

Cool hacking stuff…

Posted by jpluimers on 2020/05/06

Boy, it is indeed a game of walls and ladders:

–jeroen

Posted in CommandLine, Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »

Mad With PowerShell

Posted by jpluimers on 2020/02/19

This is such a cool blog: Mad With PowerShell (Tim Curwick’s PowerShell blog, tips and tricks, tools and techniques, explanations and explorations).

I bumped into it by finding these two:

What I like most is that it gives great insight on how and why the internals of PowerShell work the way they do, and how to use that to your advantage.

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

 
%d bloggers like this: