@@ -289,10 +289,18 @@ internal MatchInfo Clone()
289289 /// <summary>
290290 /// A cmdlet to search through strings and files for particular patterns.
291291 /// </summary>
292- [ Cmdlet ( VerbsCommon . Select , "String" , DefaultParameterSetName = "File" , HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113388" ) ]
293- [ OutputType ( typeof ( MatchInfo ) , typeof ( bool ) ) ]
292+ [ Cmdlet ( VerbsCommon . Select , "String" , DefaultParameterSetName = ParameterSetFile , HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113388" ) ]
293+ [ OutputType ( typeof ( bool ) , typeof ( MatchInfo ) , ParameterSetName = new [ ] { ParameterSetFile , ParameterSetObject , ParameterSetLiteralFile } ) ]
294+ [ OutputType ( typeof ( string ) , ParameterSetName = new [ ] { ParameterSetFileRaw , ParameterSetObjectRaw , ParameterSetLiteralFileRaw } ) ]
294295 public sealed class SelectStringCommand : PSCmdlet
295296 {
297+ private const string ParameterSetFile = "File" ;
298+ private const string ParameterSetFileRaw = "FileRaw" ;
299+ private const string ParameterSetObject = "Object" ;
300+ private const string ParameterSetObjectRaw = "ObjectRaw" ;
301+ private const string ParameterSetLiteralFile = "LiteralFile" ;
302+ private const string ParameterSetLiteralFileRaw = "LiteralFileRaw" ;
303+
296304 /// <summary>
297305 /// A generic circular buffer.
298306 /// </summary>
@@ -965,7 +973,8 @@ void IContextTracker.TrackEOF()
965973 /// <summary>
966974 /// Gets or sets the current pipeline object.
967975 /// </summary>
968- [ Parameter ( ValueFromPipeline = true , Mandatory = true , ParameterSetName = "Object" ) ]
976+ [ Parameter ( ValueFromPipeline = true , Mandatory = true , ParameterSetName = ParameterSetObject ) ]
977+ [ Parameter ( ValueFromPipeline = true , Mandatory = true , ParameterSetName = ParameterSetObjectRaw ) ]
969978 [ AllowNull ]
970979 [ AllowEmptyString ]
971980 public PSObject InputObject
@@ -988,15 +997,17 @@ public PSObject InputObject
988997 /// Gets or sets files to read from.
989998 /// Globbing is done on these.
990999 /// </summary>
991- [ Parameter ( Position = 1 , Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = "File" ) ]
1000+ [ Parameter ( Position = 1 , Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = ParameterSetFile ) ]
1001+ [ Parameter ( Position = 1 , Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = ParameterSetFileRaw ) ]
9921002 [ FileinfoToString ]
9931003 public string [ ] Path { get ; set ; }
9941004
9951005 /// <summary>
9961006 /// Gets or sets literal files to read from.
9971007 /// Globbing is not done on these.
9981008 /// </summary>
999- [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = "LiteralFile" ) ]
1009+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = ParameterSetLiteralFile ) ]
1010+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , ParameterSetName = ParameterSetLiteralFileRaw ) ]
10001011 [ FileinfoToString ]
10011012 [ Alias ( "PSPath" , "LP" ) ]
10021013 public string [ ] LiteralPath
@@ -1011,6 +1022,15 @@ public string[] LiteralPath
10111022
10121023 private bool _isLiteralPath ;
10131024
1025+ /// <summary>
1026+ /// Gets or sets a value indicating if only string values containing matched lines should be returned.
1027+ /// If not (default) return MatchInfo (or bool objects, when Quiet is passed).
1028+ /// </summary>
1029+ [ Parameter ( Mandatory = true , ParameterSetName = ParameterSetObjectRaw ) ]
1030+ [ Parameter ( Mandatory = true , ParameterSetName = ParameterSetFileRaw ) ]
1031+ [ Parameter ( Mandatory = true , ParameterSetName = ParameterSetLiteralFileRaw ) ]
1032+ public SwitchParameter Raw { get ; set ; }
1033+
10141034 /// <summary>
10151035 /// Gets or sets a value indicating if a pattern string should be matched literally.
10161036 /// If not (default) search using pattern as a Regular Expression.
@@ -1028,7 +1048,9 @@ public string[] LiteralPath
10281048 /// Gets or sets a value indicating if the cmdlet will stop processing at the first successful match and
10291049 /// return true. If both List and Quiet parameters are given, an exception is thrown.
10301050 /// </summary>
1031- [ Parameter ]
1051+ [ Parameter ( ParameterSetName = ParameterSetObject ) ]
1052+ [ Parameter ( ParameterSetName = ParameterSetFile ) ]
1053+ [ Parameter ( ParameterSetName = ParameterSetLiteralFile ) ]
10321054 public SwitchParameter Quiet { get ; set ; }
10331055
10341056 /// <summary>
@@ -1160,7 +1182,10 @@ public string[] Exclude
11601182
11611183 private int _postContext = 0 ;
11621184
1163- private IContextTracker GetContextTracker ( ) => ( _preContext == 0 && _postContext == 0 ) ? _noContextTracker : new ContextTracker ( _preContext , _postContext ) ;
1185+ // When we are in Raw mode or pre- and postcontext are zero, use the _noContextTracker, since we will not be needing trackedLines.
1186+ private IContextTracker GetContextTracker ( ) => ( Raw || ( _preContext == 0 && _postContext == 0 ) )
1187+ ? _noContextTracker
1188+ : new ContextTracker ( _preContext , _postContext ) ;
11641189
11651190 // This context tracker is only used for strings which are piped
11661191 // directly into the cmdlet. File processing doesn't need
@@ -1425,8 +1450,14 @@ private bool FlushTrackerQueue(IContextTracker contextTracker)
14251450 return false ;
14261451 }
14271452
1428- // If -quiet is specified but not -list return true on first match
1429- if ( Quiet && ! List )
1453+ if ( Raw )
1454+ {
1455+ foreach ( MatchInfo match in contextTracker . EmitQueue )
1456+ {
1457+ WriteObject ( match . Line ) ;
1458+ }
1459+ }
1460+ else if ( Quiet && ! List )
14301461 {
14311462 WriteObject ( true ) ;
14321463 }
0 commit comments