header image

Archive for August, 2018

PowerShell string contains

Posted by: | August 31, 2018 Comments Off on PowerShell string contains |

How can you check if a PowerShell string contains a character or substring? You might be tempted to try this: PS> $s = ‘abcdefghijk’ PS> $s -contains ‘f’ False   But –contains is for working with the contents of arrays. So you could do this: PS> ($s.ToCharArray()) -contains ‘f’ True   You’re implicitly converting the […]

under: PowerShell

PowerShell Day UK Agenda Update

Posted by: | August 28, 2018 Comments Off on PowerShell Day UK Agenda Update |

There’s been a Powershell Day UK Agenda Update.   Looks like there’ll be three tracks for most of the day.   Full agenda and tickets from – https://psday.uk/    

under: PowerShell

PowerShell v6.1– using PowerShell v5.1 modules

Posted by: | August 23, 2018 Comments Off on PowerShell v6.1– using PowerShell v5.1 modules |

Something new to be aware of in PowerShell v6.1– using PowerShell v5.1 modules . In PowerShell v6.0.4 the default module path is PS> $env:PSModulePath -split ‘;’ C:\Users\Richard\Documents\PowerShell\Modules C:\Program Files\PowerShell\Modules c:\program files\powershell\6.0.4\Modules   If you want to use the netadapter cmdlets for instance to need to explicitly load the module Import-Module ‘C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\netadapter\netadapter.psd1’ You can then use […]

under: PowerShell v6

PowerShell v6.1 release candidate 1

Posted by: | August 23, 2018 Comments Off on PowerShell v6.1 release candidate 1 |

PowerShell v6.1 release candidate 1 is now available from https://github.com/PowerShell/PowerShell/releases The release notes don’t show any major new functionality compared to preview 4 though there are a number of minor updates, changes and bug fixes.

under: PowerShell v6

Deleting folders based on month name

Posted by: | August 20, 2018 Comments Off on Deleting folders based on month name |

An interesting problem around deleting folders based on month name. You have a folder structure that looks like this C:\Testdata\2011\08-Aug C:\Testdata\2011\09-Sep C:\Testdata\2011\10-Oct C:\Testdata\2011\11-Nov C:\Testdata\2011\12-Dec C:\Testdata\2012\01-Jan C:\Testdata\2012\02-Feb C:\Testdata\2012\03-Mar C:\Testdata\2012\04-Apr C:\Testdata\2012\05-May C:\Testdata\2012\06-Jun C:\Testdata\2012\07-Jul C:\Testdata\2012\08-Aug C:\Testdata\2012\09-Sep C:\Testdata\2012\10-Oct C:\Testdata\2012\11-Nov C:\Testdata\2012\12-Dec C:\Testdata\2013\01-Jan C:\Testdata\2013\02-Feb C:\Testdata\2013\03-Mar C:\Testdata\2013\04-Apr C:\Testdata\2013\05-May C:\Testdata\2013\06-Jun C:\Testdata\2013\07-Jul C:\Testdata\2013\08-Aug   The structure extends forward and backward in time. Each year […]

under: PowerShell

File searches with WMI

Posted by: | August 19, 2018 Comments Off on File searches with WMI |

I saw a question about file searches with WMI. If you just know the file name it’s a very slow process. Painfully slow. If you have an idea about the folder its much quicker.   function get-wmifile { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$path, [string]$file ) if ($path.IndexOf(‘\\’) -le 0 ){ $path = $path.replace(‘\’, […]

under: PowerShell and CIM, PowerShell and WMI

Variable type

Posted by: | August 18, 2018 Comments Off on Variable type |

Usually when you create a variable you implicitly set the type by the value you use. Sometimes though you may want to explicitly set the variable type.   if you don’t give the variable a type you can do this: PS> $x = 35 PS> $x 35 PS> $x = ‘now a string’ PS> $x […]

under: PowerShell

Read only and constant variables

Posted by: | August 16, 2018 Comments Off on Read only and constant variables |

When you create a variable you usually want to be able to change its value – the clue is in the term variable. Sometimes though you might want to use a value that doesn’t change – you want read only and constant variables. Standard variables are changeable: PS> $x = 10 PS> $x 10 PS> […]

under: PowerShell

user logon time

Posted by: | August 15, 2018 Comments Off on user logon time |

Saw an interesting question about user logon time. How can you tell the logged on user and when they logged on   $logon = Get-CimInstance -ClassName Win32_LogonSession | sort StartTime -Descending | select -First 1 $user = Get-CimAssociatedInstance -InputObject $logon -ResultClassName Win32_Account $props = [ordered]@{ Name = $user.Fullname UserId = $user.Name Domain = $user.Domain LocalAccount […]

under: PowerShell and CIM, PowerShell and WMI

Splitting on a \

Posted by: | August 15, 2018 Comments Off on Splitting on a \ |

Splitting strings is usually easy – you use the –split operator or the Split() method. Occasionally you may hit a problem – for instance splitting on a \ character. Let me demonstrate with an example. PS> Get-CimInstance -ClassName Win32_ComputerSystem | select Username Username ——– W510W10\Richard   I get the domain (in this case the machine […]

under: PowerShell

Older Posts »

Categories