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 […]
Archive for August, 2018
PowerShell string contains
Posted by: richardsiddaway | August 31, 2018 Comments Off on PowerShell string contains |PowerShell Day UK Agenda Update
Posted by: richardsiddaway | 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/
PowerShell v6.1– using PowerShell v5.1 modules
Posted by: richardsiddaway | 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 […]
PowerShell v6.1 release candidate 1
Posted by: richardsiddaway | 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.
Deleting folders based on month name
Posted by: richardsiddaway | 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 […]
File searches with WMI
Posted by: richardsiddaway | 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(‘\’, […]
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 […]
Read only and constant variables
Posted by: richardsiddaway | 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> […]
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 […]
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 […]
Categories
- .NET
- Active Directory
- Architecture
- Azure
- Bash
- BITS
- Books
- CDXML
- CIM
- Cloud
- COM
- Containers
- Deep Dive
- Desired State Configuration
- DevOps
- DHCP
- DNS
- DSC
- European Summit
- Events
- Exchange
- File System
- Firewall
- General
- General IT Matters
- Hyper-V
- IIS
- Infrastructure
- IT Community
- IT Security
- Learning PowerShell
- Linux
- Math
- Microsoft
- Modules
- Nano Server
- Networking
- Office 2010
- Office 2013
- Open Source
- Opinion
- Outlook
- Philosophy
- PowerShell
- PowerShell 7
- PowerShell and .NET
- PowerShell and Active Directory
- PowerShell and CIM
- PowerShell and Exchange 2007
- PowerShell and IIS
- PowerShell and SQL Server
- PowerShell and WMI
- PowerShell Basics
- PowerShell original
- PowerShell Summit
- PowerShell User Group
- PowerShell User Group 2
- PowerShell v2
- PowerShell V3
- PowerShell v4
- PowerShell v5
- PowerShell v6
- PowerShell.org
- PowerShellGet
- PowerShellV2
- PSAM
- Rant
- Registry
- Scripting
- Scripting Games
- Scripting Games 2104
- Security
- SQL Server
- Storage
- Strings
- Summit
- Technology
- Uncategorized
- Virtualization
- Windows 10
- Windows 2012 R2
- Windows 7
- Windows 8
- Windows 8 Server
- Windows 8.1
- Windows Server
- Windows server 1709
- Windows Server 2008
- Windows Server 2008 R2
- Windows Server 2012
- Windows Server 2012 R2
- Windows Server 2016
- Windows Server 2019
- WMFv5
- WPF
- WSUS