Home
%3CLINGO-SUB%20id%3D%22lingo-sub-366659%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-366659%22%20slang%3D%22en-US%22%3E%3CP%3E%24dir.trimend('%5C')%20%2B%20'%5C'%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308678%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308678%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F242302%22%20target%3D%22_blank%22%3E%40Joel%20Francis%3C%2FA%3E%26nbsp%3BI%20just%20installed%20PS%20Core%206.1.1%20and%20got%20similar%20results%20as%20you.%20I%20also%20tested%20your%20array%20index%20method%20and%20it's%20even%20faster%20than%20.EndsWith()%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ERegEx%20method%3A%20125%20ms%3CBR%20%2F%3EEndsWith%20method%3A%2017%20ms%3CBR%20%2F%3ELike%20method%3A%2052%20ms%3CBR%20%2F%3EArray%20index%20method%3A%209%20ms%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3ECode%20for%20array%20index%20method%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24stopwatch%20%3D%20%5BStopwatch%5D%3A%3AStartNew()%3B%0Afor%20(%24i%20%3D%200%3B%20%24i%20-lt%20100000%3B%20%24i%2B%2B)%20%7B%0A%20%20%20%20%24boolValue%20%3D%20'something%5C'%5B-1%5D%20-eq%20'%5C'%0A%7D%0A%22Array%20index%20method%3A%20%24(%24stopwatch.ElapsedMilliseconds)%20ms%20%22%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20the%20times%20in%20my%20previous%26nbsp%3Bcomment%20were%20generated%20using%26nbsp%3Bthe%20play%20button%20(Run%20Script)%20in%20Windows%20PowerShell%20ISE%2C%20which%20adds%20debugging%20overhead.%20When%20I%20run%20it%20directly%20in%20PowerShell.exe%20(5.1.1)%20I%20get%20similar%20results%20to%20PS%20Core%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ERegEx%20method%3A%20127%20ms%20%0AEndsWith%20method%3A%2023%20ms%20%0ALike%20method%3A%2044%20ms%20%0AArray%20index%20method%3A%2014%20ms%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(In%20the%20end%2C%20unless%20you're%20checking%20the%20ends%20of%20%3CEM%3Emany%3C%2FEM%3E%20strings%20or%20performance%20matters%20a%20lot%2C%20it%20doesn't%20really%20matter%20what%20method%20you%20use.%20Personally%20I'll%20probably%20stick%20with%20-Like%20because%20I%20like%20it.%20%3B)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308619%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308619%22%20slang%3D%22en-US%22%3E%3CP%3EQuick%20comparison%20of%20100%20iterations%20of%20testing%20a%20simple%20string%20%22hello%22%20and%20checking%20for%20the%20last%20letter%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24string.EndsWith(%22o%22)%20-%2010.5%20ms%0A%24string%20-like%20%22*o%22%20-%2011%20ms%0A%24string%20-match%20%22o%24%22%20-%2035%20ms%0A%24string%5B-1%5D%20-eq%20%22o%22%20-%2014%20ms%3C%2FPRE%3E%3CP%3EUsing%20Measure-Command.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E.EndsWith()%20is%20the%20quickest%20method%20in%20most%20cases%2C%20I%20would%20venture.%20Ran%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F102838%22%20target%3D%22_blank%22%3E%40Anthony%20Bartolo%3C%2FA%3E's%20code%20and%20got%20the%20following%20results%20in%20PS%20Core%206.1.0%3A%3C%2FP%3E%3CPRE%3ERegEx%20method%3A%201229%20ms%0AEndsWith%20method%3A%20459%20ms%0ALike%20method%3A%20594%20ms%3C%2FPRE%3E%3CP%3E(With%20a%20rather%20lacklustre%20machine%20%3B)%3C%2Fimg%3E%20)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308615%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308615%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20Windows%20PowerShell%205.1%2C%26nbsp%3BEndsWith()%20and%20-like%20are%20almost%20twice%20as%20fast%20as%20RegEx%2C%20across%20100%2C000%20iterations%20each%20(using%20the%20code%20below).%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F260156%22%20target%3D%22_blank%22%3E%40TheDammedGamer%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ERegEx%20method%3A%20295%20ms%0AEndsWith%20method%3A%20189%20ms%0ALike%20method%3A%20160%20ms%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Eusing%20namespace%20System.Diagnostics%0A%0A%24stopwatch%20%3D%20%5BStopwatch%5D%3A%3AStartNew()%3B%0Afor%20(%24i%20%3D%200%3B%20%24i%20-lt%20100000%3B%20%24i%2B%2B)%20%7B%0A%20%20%20%20%24boolValue%20%3D%20'something%5C'%20-match%20'%5C%5C%24'%0A%7D%0A%22RegEx%20method%3A%20%24(%24stopwatch.ElapsedMilliseconds)%20ms%20%22%0A%0A%24stopwatch%20%3D%20%5BStopwatch%5D%3A%3AStartNew()%3B%0Afor%20(%24i%20%3D%200%3B%20%24i%20-lt%20100000%3B%20%24i%2B%2B)%20%7B%0A%20%20%20%20%24boolValue%20%3D%20'something%5C'.EndsWith('%5C')%0A%7D%0A%22EndsWith%20method%3A%20%24(%24stopwatch.ElapsedMilliseconds)%20ms%20%22%0A%0A%24stopwatch%20%3D%20%5BStopwatch%5D%3A%3AStartNew()%3B%0Afor%20(%24i%20%3D%200%3B%20%24i%20-lt%20100000%3B%20%24i%2B%2B)%20%7B%0A%20%20%20%20%24boolValue%20%3D%20'something%5C'%20-like%20'*%5C'%0A%7D%0A%22Like%20method%3A%20%24(%24stopwatch.ElapsedMilliseconds)%20ms%20%22%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308593%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308593%22%20slang%3D%22en-US%22%3E%3CP%3EQuick%20question%2C%20is%20this%20faster%20than%20the%20in-built%20%24var.EndsWith(%22string%22)%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308445%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308445%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F259792%22%20target%3D%22_blank%22%3E%40Tonedef%3C%2FA%3E%20Thank%20you%20for%20the%20share.%26nbsp%3B%20Let%20me%20know%20if%20there%20are%20any%20other%20PowerShell%20tips%20you'd%20be%20interested%20in%20use%20researching%20and%20sharing.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308237%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308237%22%20slang%3D%22en-US%22%3E%3CP%3EAnother%20method%20(and%20the%20one%20I%20prefer)%20is%20to%20use%20the%20-Like%20comparison%20operator%3A%3C%2FP%3E%3CPRE%3Eif%20(%22something%5C%22%20-like%20%22*%5C%22)%20%7B%0A%20%20%20%20Write-Host%20%22Ends%20in%20a%20backslash.%22%0A%7D%0Aelse%20%7B%0A%20%20%20%20Write-Host%20%22Does%20not%20end%20in%20a%20backslash.%22%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20this%20method%20you%20don't%20need%20to%20escape%20the%20backslash%2C%20and%20it%20uses%20an%20asterisk%20to%20match%20any%20text%20(or%20none)%20before%20the%20backslash.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308134%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308134%22%20slang%3D%22en-US%22%3E%3CP%3ENice.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308124%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308124%22%20slang%3D%22en-US%22%3E%3CP%3EI%20don't%20think%20this%20situation%20really%20calls%20for%20regex.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPowerShell%20is%20capable%20of%20indexing%20a%20string%20just%20like%20a%20character%20array%2C%20and%20its%20comparison%20system%20can%20convert%20data%20types%20when%20needed.%20As%20a%20result%2C%20you%20can%20do%20something%20very%20straightforward%20like%20this%3A%3C%2FP%3E%3CPRE%3E%24String%20%3D%20%22When%20Lords%20and%20Ladies%20quest%20for%20fame%2C%20a%20Beast%20will%20touch%20the%20land%20with%20flame.%22%0A%0Aif%20(%20%24String%5B-1%5D%20-eq%20'.'%20)%20%7B%0A%26nbsp%3B%20%26nbsp%3B%20%23%20Do%20something%0A%7D%3CBR%20%2F%3Eelse%20%7B%3CBR%20%2F%3E%20%20%20%20%23%20Do%20something%20else%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3EAlso%2C%20if%20this%20is%20about%20checking%20if%20a%20path%20is%20a%20container%20or%20leaf%20object%2C%20you%20can%20do%20that%26nbsp%3B%3CEM%3Emuch%3C%2FEM%3E%20more%20effectively%2C%20thoroughly%2C%20and%20verbosely%20like%20this%3A%3C%2FP%3E%3CPRE%3ETest-Path%20-Path%20%22C%3A%5CMyFolder%22%20-PathType%20Container%3C%2FPRE%3E%3CP%3EAnd%20if%20the%20path%20might%20not%20exist%20you%20can%20add%26nbsp%3B%3CSTRONG%3E-IsValid%3C%2FSTRONG%3E%20to%20that%20call%2C%20although%20in%20that%20case%20I%20believe%20the%20-PathType%20isn't%20particularly%20useful%20there%20and%20then%2C%20perhaps%2C%20some%20more%20manual%20checking%20might%20be%20a%20bit%20more%20useful%20depending%20on%20your%20specific%20use%20case.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308107%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308107%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F259687%22%20target%3D%22_blank%22%3E%40cjcox%3C%2FA%3E%20updated%20as%20per%20your%20suggestion.%26nbsp%3B%20Thank%20you%20for%20the%20tip!%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-308099%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-308099%22%20slang%3D%22en-US%22%3EThere's%20no%20need%20for%20.%2B%3F.%20You%20could%20just%20use%20the%20pattern%20'%5C%5C%24'%20to%20match%20a%20backslash%20at%20the%20end.%20Ditto%20for%20begins%20with%20'%5E%5C%5C'.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307848%22%20slang%3D%22en-US%22%3EPowerShell%20Basics%3A%20Detecting%20if%20a%20String%20Ends%20with%20a%20Certain%20Character%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307848%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EDid%20you%20know%20you%20can%20detect%20if%20a%20string%20ends%20in%20a%20specific%20character%20or%20if%20it%20starts%20in%20one%20in%20PowerShell%3F%20Thomas%20Rayner%20previously%20shared%20on%20CANITPRO.NET%20how%20this%20can%20be%20easily%20done%20by%20using%20regular%20expressions%20or%20more%20simply%20know%20as%20Regex.%26nbsp%3B%3C%2FSPAN%3E%26nbsp%3BConsider%20the%20following%20examples%3A%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22brush%3A%20powershell%3B%22%3E'something%5C'%20-match%20'%5C%5C%24'%0A%23returns%20true%0A%0A'something'%20-match%20'%5C%5C%24'%0A%23returns%20false%0A%0A'%5Csomething'%20-match%20'%5E%5C%5C'%0A%23returns%20true%0A%0A'something'%20-match%20'%5E%5C%5C'%0A%23returns%20false%0A%0A%3C%2FPRE%3E%0A%3CP%3EIn%20the%20first%20two%20examples%2C%20the%20script%20checks%20the%20string%20ends%20to%20see%20if%20it%20ends%20in%20a%20backslash.%20In%20the%20last%20two%20examples%2C%20the%20script%20check%20the%20string%20to%20see%20if%20it%20starts%20with%20one.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20regex%20pattern%20being%20matched%20for%20the%20first%20two%20is%20%3CSTRONG%3E%5C%5C%24%3C%2FSTRONG%3E%26nbsp%3B.%20What%E2%80%99s%20that%20mean%3F%20Well%2C%20the%20first%20part%20%3CSTRONG%3E%5C%5C%3C%2FSTRONG%3E%26nbsp%3Bmeans%20%E2%80%9Ca%20backslash%E2%80%9D%20(because%20%5C%20is%20the%20escape%20character%2C%20we%E2%80%99re%20basically%20escaping%20the%20escape%20character.%20The%20last%20part%26nbsp%3B%3CSTRONG%3E%24%3C%2FSTRONG%3E%20is%20the%20signal%20for%20the%20end%20of%20the%20line.%20Effectively%20what%20we%20have%20is%20%E2%80%9Canything%20at%20all%2C%20where%20the%20last%20thing%20on%20the%20line%20is%20a%20backslash%E2%80%9D%20which%20is%20exactly%20what%20we%E2%80%99re%20looking%20for.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20second%20two%20examples%2C%20I%E2%80%99ve%20just%20moved%20the%20%3CSTRONG%3E%5C%5C%3C%2FSTRONG%3E%20to%20the%20start%20of%20the%20line%20and%20started%20with%26nbsp%3B%3CSTRONG%3E%5E%3C%2FSTRONG%3E%20instead%20of%20ending%20with%26nbsp%3B%3CSTRONG%3E%24%3C%2FSTRONG%3E%20because%26nbsp%3B%3CSTRONG%3E%5E%3C%2FSTRONG%3E%20is%20the%20signal%20for%20the%20start%20of%20the%20line.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20you%20can%20do%20things%20like%20this%3A%3C%2FSPAN%3E%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22brush%3A%20powershell%3B%22%3E%24dir%20%3D%20'c%3A%5Ctemp'%0Aif%20(%24dir%20-notmatch%20'%5C%5C%24')%0A%7B%0A%24dir%20%2B%3D%20'%5C'%0A%7D%0A%24dir%0A%23returns%20'c%3A%5Ctemp%5C'%0A%3C%2FPRE%3E%0A%3CP%3EHere%2C%20the%20script%20checks%20to%20see%20if%20the%20string%20%E2%80%98bears%E2%80%99%20ends%20in%20a%20backslash%2C%20and%20if%20it%20doesn%E2%80%99t%2C%20I%E2%80%99m%20appending%20one.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F64467i59DAE518FEDCC930%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22PowerShell%20Basics%20Series%22%20title%3D%22PowerShell_Basics.jpg%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPowerShell%20Basics%20Series%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-307848%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EDid%20you%20know%20you%20can%20detect%20if%20a%20string%20ends%20in%20a%20specific%20character%20or%20if%20it%20starts%20in%20one%20in%20PowerShell%3F%20Thomas%20Rayner%20previously%20shared%20on%20CANITPRO.NET%20how%20this%20can%20be%20easily%20done%20by%20using%20regular%20expressions%20or%20more%20simply%20know%20as%20Regex.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F59633i420FEE9A9109E151%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22PowerShell_Basics.jpg%22%20title%3D%22PowerShell_Basics.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-307848%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAnthony%20Bartolo%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Did you know you can detect if a string ends in a specific character or if it starts in one in PowerShell? Thomas Rayner previously shared on CANITPRO.NET how this can be easily done by using regular expressions or more simply know as Regex.  Consider the following examples:
 

'something\' -match '\\$'
#returns true

'something' -match '\\$'
#returns false

'\something' -match '^\\'
#returns true

'something' -match '^\\'
#returns false

In the first two examples, the script checks the string ends to see if it ends in a backslash. In the last two examples, the script check the string to see if it starts with one.

 

The regex pattern being matched for the first two is \\$ . What’s that mean? Well, the first part \\ means “a backslash” (because \ is the escape character, we’re basically escaping the escape character. The last part $ is the signal for the end of the line. Effectively what we have is “anything at all, where the last thing on the line is a backslash” which is exactly what we’re looking for.

 

In the second two examples, I’ve just moved the \\ to the start of the line and started with ^ instead of ending with $ because ^ is the signal for the start of the line.

 

Now you can do things like this:
 

$dir = 'c:\temp'
if ($dir -notmatch '\\$')
{
$dir += '\'
}
$dir
#returns 'c:\temp\'

Here, the script checks to see if the string ‘bears’ ends in a backslash, and if it doesn’t, I’m appending one. 

 PowerShell Basics SeriesPowerShell Basics Series

 

 

 

11 Comments
Deleted
Not applicable
There's no need for .+?. You could just use the pattern '\\$' to match a backslash at the end. Ditto for begins with '^\\'.
Microsoft

@Deleted updated as per your suggestion.  Thank you for the tip!


Occasional Visitor

I don't think this situation really calls for regex.

 

PowerShell is capable of indexing a string just like a character array, and its comparison system can convert data types when needed. As a result, you can do something very straightforward like this:

$String = "When Lords and Ladies quest for fame, a Beast will touch the land with flame."

if ( $String[-1] -eq '.' ) {
    # Do something
}
else {
# Do something else
}

Also, if this is about checking if a path is a container or leaf object, you can do that much more effectively, thoroughly, and verbosely like this:

Test-Path -Path "C:\MyFolder" -PathType Container

And if the path might not exist you can add -IsValid to that call, although in that case I believe the -PathType isn't particularly useful there and then, perhaps, some more manual checking might be a bit more useful depending on your specific use case.

 

Visitor

Nice.

Visitor

Another method (and the one I prefer) is to use the -Like comparison operator:

if ("something\" -like "*\") {
    Write-Host "Ends in a backslash."
}
else {
    Write-Host "Does not end in a backslash."
}

 

With this method you don't need to escape the backslash, and it uses an asterisk to match any text (or none) before the backslash.

Microsoft

@Tonedef Thank you for the share.  Let me know if there are any other PowerShell tips you'd be interested in use researching and sharing.

Occasional Visitor

Quick question, is this faster than the in-built $var.EndsWith("string") ?

Visitor

Using Windows PowerShell 5.1, EndsWith() and -like are almost twice as fast as RegEx, across 100,000 iterations each (using the code below). @TheDammedGamer

 

RegEx method: 295 ms
EndsWith method: 189 ms
Like method: 160 ms

 

 

using namespace System.Diagnostics

$stopwatch = [Stopwatch]::StartNew();
for ($i = 0; $i -lt 100000; $i++) {
    $boolValue = 'something\' -match '\\$'
}
"RegEx method: $($stopwatch.ElapsedMilliseconds) ms "

$stopwatch = [Stopwatch]::StartNew();
for ($i = 0; $i -lt 100000; $i++) {
    $boolValue = 'something\'.EndsWith('\')
}
"EndsWith method: $($stopwatch.ElapsedMilliseconds) ms "

$stopwatch = [Stopwatch]::StartNew();
for ($i = 0; $i -lt 100000; $i++) {
    $boolValue = 'something\' -like '*\'
}
"Like method: $($stopwatch.ElapsedMilliseconds) ms "

Occasional Visitor

Quick comparison of 100 iterations of testing a simple string "hello" and checking for the last letter:

 

$string.EndsWith("o") - 10.5 ms
$string -like "*o" - 11 ms
$string -match "o$" - 35 ms
$string[-1] -eq "o" - 14 ms

Using Measure-Command.

 

.EndsWith() is the quickest method in most cases, I would venture. Ran @Anthony Bartolo's code and got the following results in PS Core 6.1.0:

RegEx method: 1229 ms
EndsWith method: 459 ms
Like method: 594 ms

(With a rather lacklustre machine ;) )

Visitor

@Joel Francis I just installed PS Core 6.1.1 and got similar results as you. I also tested your array index method and it's even faster than .EndsWith():

 

RegEx method: 125 ms
EndsWith method: 17 ms
Like method: 52 ms
Array index method: 9 ms

  

Code for array index method:

 

$stopwatch = [Stopwatch]::StartNew();
for ($i = 0; $i -lt 100000; $i++) {
    $boolValue = 'something\'[-1] -eq '\'
}
"Array index method: $($stopwatch.ElapsedMilliseconds) ms "

 

Also, the times in my previous comment were generated using the play button (Run Script) in Windows PowerShell ISE, which adds debugging overhead. When I run it directly in PowerShell.exe (5.1.1) I get similar results to PS Core:

 

RegEx method: 127 ms 
EndsWith method: 23 ms 
Like method: 44 ms 
Array index method: 14 ms 

 

(In the end, unless you're checking the ends of many strings or performance matters a lot, it doesn't really matter what method you use. Personally I'll probably stick with -Like because I like it. ;)

Occasional Visitor

$dir.trimend('\') + '\'