From d76d19c9085fe5a5e2547a70153603903bd155e8 Mon Sep 17 00:00:00 2001 From: Sean Wheeler Date: Fri, 5 Nov 2021 09:14:26 -0500 Subject: [PATCH 1/3] Fix description of final example (#8310) --- .../configurations/add-parameters-to-a-configuration.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/reference/docs-conceptual/dsc/configurations/add-parameters-to-a-configuration.md b/reference/docs-conceptual/dsc/configurations/add-parameters-to-a-configuration.md index 73115d9b32e5..c3341e5203af 100644 --- a/reference/docs-conceptual/dsc/configurations/add-parameters-to-a-configuration.md +++ b/reference/docs-conceptual/dsc/configurations/add-parameters-to-a-configuration.md @@ -1,6 +1,6 @@ --- description: DSC Configurations can be parameterized to allow more dynamic configurations based on user input. -ms.date: 12/12/2018 +ms.date: 11/05/2021 title: Add Parameters to a Configuration --- @@ -44,7 +44,7 @@ Configurations can also use the following built in parameters, without requiring | `-DependsOn` | Used in defining [Composite Configurations](compositeconfigs.md) | | `-PSDSCRunAsCredential` | Used in defining [Composite Configurations](compositeconfigs.md) | | `-ConfigurationData` | Used to pass in structured [Configuration Data](configData.md) for use in the Configuration. | -| `-OutputPath` | Used to specify where your "\.mof" file will be compiled | +| `-OutputPath` | Used to specify where your `.mof` file will be compiled | ## Adding your own parameters to Configurations @@ -215,8 +215,8 @@ You can read more about the `parameter` and validation attributes in ## Fully parameterized Configuration -We now have a parameterized Configuration that forces the user to specify an `-InstanceName`, -`-ServiceName`, and validates the `-State` parameter. +We now have a parameterized Configuration that forces the user to specify a `-ServiceName`, +optionally provide a `-ComputerName`, and validates the value of the `-State` parameter. ```powershell Configuration TestConfig From 760e027cddfe3f14e604b6845f9bc0b017e54e86 Mon Sep 17 00:00:00 2001 From: Matt Gucci Date: Fri, 5 Nov 2021 23:33:59 +0900 Subject: [PATCH 2/3] Remove duplicate: AccessDBProviderSample03.cs (#8307) --- .../accessdbprovidersample03-code-sample.md | 969 ------------------ 1 file changed, 969 deletions(-) diff --git a/reference/docs-conceptual/developer/prog-guide/accessdbprovidersample03-code-sample.md b/reference/docs-conceptual/developer/prog-guide/accessdbprovidersample03-code-sample.md index b40546c7e516..38069e83be6b 100644 --- a/reference/docs-conceptual/developer/prog-guide/accessdbprovidersample03-code-sample.md +++ b/reference/docs-conceptual/developer/prog-guide/accessdbprovidersample03-code-sample.md @@ -21,975 +21,6 @@ This provider that can manipulate the data in a data store. ## Code Sample -```csharp -using System; -using System.IO; -using System.Data; -using System.Data.Odbc; -using System.Collections.ObjectModel; -using System.Text; -using System.Diagnostics; -using System.Text.RegularExpressions; -using System.Management.Automation; -using System.Management.Automation.Provider; -using System.ComponentModel; -using System.Globalization; - -namespace Microsoft.Samples.PowerShell.Providers -{ - #region AccessDBProvider - - /// - /// A PowerShell Provider which acts upon a access database. - /// - /// - /// This example implements the item overloads. - /// - [CmdletProvider("AccessDB", ProviderCapabilities.None)] - - public class AccessDBProvider : ItemCmdletProvider - { - #region Drive Manipulation - - /// - /// Create a new drive. Create a connection to the database file and set - /// the Connection property in the PSDriveInfo. - /// - /// - /// Information describing the drive to add. - /// - /// The added drive. - protected override PSDriveInfo NewDrive(PSDriveInfo drive) - { - // check if drive object is null - if (drive == null) - { - WriteError(new ErrorRecord( - new ArgumentNullException("drive"), - "NullDrive", - ErrorCategory.InvalidArgument, - null) - ); - - return null; - } - - // check if drive root is not null or empty - // and if its an existing file - if (String.IsNullOrEmpty(drive.Root) || (File.Exists(drive.Root) == false)) - { - WriteError(new ErrorRecord( - new ArgumentException("drive.Root"), - "NoRoot", - ErrorCategory.InvalidArgument, - drive) - ); - - return null; - } - - // create a new drive and create an ODBC connection to the new drive - AccessDBPSDriveInfo accessDBPSDriveInfo = new AccessDBPSDriveInfo(drive); - - OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(); - - builder.Driver = "Microsoft Access Driver (*.mdb)"; - builder.Add("DBQ", drive.Root); - - OdbcConnection conn = new OdbcConnection(builder.ConnectionString); - conn.Open(); - accessDBPSDriveInfo.Connection = conn; - - return accessDBPSDriveInfo; - } // NewDrive - - /// - /// Removes a drive from the provider. - /// - /// The drive to remove. - /// The drive removed. - protected override PSDriveInfo RemoveDrive(PSDriveInfo drive) - { - // check if drive object is null - if (drive == null) - { - WriteError(new ErrorRecord( - new ArgumentNullException("drive"), - "NullDrive", - ErrorCategory.InvalidArgument, - drive) - ); - - return null; - } - - // close ODBC connection to the drive - AccessDBPSDriveInfo accessDBPSDriveInfo = drive as AccessDBPSDriveInfo; - - if (accessDBPSDriveInfo == null) - { - return null; - } - accessDBPSDriveInfo.Connection.Close(); - - return accessDBPSDriveInfo; - } // RemoveDrive - - #endregion Drive Manipulation - - #region Item Methods - - /// - /// Retrieves an item using the specified path. - /// - /// The path to the item to return. - protected override void GetItem(string path) - { - // check if the path represented is a drive - if (PathIsDrive(path)) - { - WriteItemObject(this.PSDriveInfo, path, true); - return; - }// if (PathIsDrive... - - // Get table name and row information from the path and do - // necessary actions - string tableName; - int rowNumber; - - PathType type = GetNamesFromPath(path, out tableName, out rowNumber); - - if (type == PathType.Table) - { - DatabaseTableInfo table = GetTable(tableName); - WriteItemObject(table, path, true); - } - else if (type == PathType.Row) - { - DatabaseRowInfo row = GetRow(tableName, rowNumber); - WriteItemObject(row, path, false); - } - else - { - ThrowTerminatingInvalidPathException(path); - } - - } // GetItem - - /// - /// Set the content of a row of data specified by the supplied path - /// parameter. - /// - /// Specifies the path to the row whose columns - /// will be updated. - /// Comma separated string of values - protected override void SetItem(string path, object values) - { - // Get type, table name and row number from the path specified - string tableName; - int rowNumber; - - PathType type = GetNamesFromPath(path, out tableName, out rowNumber); - - if (type != PathType.Row) - { - WriteError(new ErrorRecord(new NotSupportedException( - "SetNotSupported"), "", - ErrorCategory.InvalidOperation, path)); - - return; - } - - // Get in-memory representation of table - OdbcDataAdapter da = GetAdapterForTable(tableName); - - if (da == null) - { - return; - } - DataSet ds = GetDataSetForTable(da, tableName); - DataTable table = GetDataTable(ds, tableName); - - if (rowNumber >= table.Rows.Count) - { - // The specified row number has to be available. If not - // NewItem has to be used to add a new row - throw new ArgumentException("Row specified is not available"); - } // if (rowNum... - - string[] colValues = (values as string).Split(','); - - // set the specified row - DataRow row = table.Rows[rowNumber]; - - for (int i = 0; i < colValues.Length; i++) - { - row[i] = colValues[i]; - } - - // Update the table - if (ShouldProcess(path, "SetItem")) - { - da.Update(ds, tableName); - } - - } // SetItem - - /// - /// Test to see if the specified item exists. - /// - /// The path to the item to verify. - /// True if the item is found. - protected override bool ItemExists(string path) - { - // check if the path represented is a drive - if (PathIsDrive(path)) - { - return true; - } - - // Obtain type, table name and row number from path - string tableName; - int rowNumber; - - PathType type = GetNamesFromPath(path, out tableName, out rowNumber); - - DatabaseTableInfo table = GetTable(tableName); - - if (type == PathType.Table) - { - // if specified path represents a table then DatabaseTableInfo - // object for the same should exist - if (table != null) - { - return true; - } - } - else if (type == PathType.Row) - { - // if specified path represents a row then DatabaseTableInfo should - // exist for the table and then specified row number must be within - // the maximum row count in the table - if (table != null && rowNumber < table.RowCount) - { - return true; - } - } - - return false; - - } // ItemExists - - /// - /// Test to see if the specified path is syntactically valid. - /// - /// The path to validate. - /// True if the specified path is valid. - protected override bool IsValidPath(string path) - { - bool result = true; - - // check if the path is null or empty - if (String.IsNullOrEmpty(path)) - { - result = false; - } - - // convert all separators in the path to a uniform one - path = NormalizePath(path); - - // split the path into individual chunks - string[] pathChunks = path.Split(pathSeparator.ToCharArray()); - - foreach (string pathChunk in pathChunks) - { - if (pathChunk.Length == 0) - { - result = false; - } - } - return result; - } // IsValidPath - - #endregion Item Overloads - - #region Helper Methods - - /// - /// Checks if a given path is actually a drive name. - /// - /// The path to check. - /// - /// True if the path given represents a drive, false otherwise. - /// - private bool PathIsDrive(string path) - { - // Remove the drive name and first path separator. If the - // path is reduced to nothing, it is a drive. Also if its - // just a drive then there wont be any path separators - if (String.IsNullOrEmpty( - path.Replace(this.PSDriveInfo.Root, "")) || - String.IsNullOrEmpty( - path.Replace(this.PSDriveInfo.Root + pathSeparator, "")) - - ) - { - return true; - } - else - { - return false; - } - } // PathIsDrive - - /// - /// Breaks up the path into individual elements. - /// - /// The path to split. - /// An array of path segments. - private string[] ChunkPath(string path) - { - // Normalize the path before splitting - string normalPath = NormalizePath(path); - - // Return the path with the drive name and first path - // separator character removed, split by the path separator. - string pathNoDrive = normalPath.Replace(this.PSDriveInfo.Root - + pathSeparator, ""); - - return pathNoDrive.Split(pathSeparator.ToCharArray()); - } // ChunkPath - - /// - /// Adapts the path, making sure the correct path separator - /// character is used. - /// - /// - /// - private string NormalizePath(string path) - { - string result = path; - - if (!String.IsNullOrEmpty(path)) - { - result = path.Replace("/", pathSeparator); - } - - return result; - } // NormalizePath - - /// - /// Chunks the path and returns the table name and the row number - /// from the path - /// - /// Path to chunk and obtain information - /// Name of the table as represented in the - /// path - /// Row number obtained from the path - /// what the path represents - private PathType GetNamesFromPath(string path, out string tableName, out int rowNumber) - { - PathType retVal = PathType.Invalid; - rowNumber = -1; - tableName = null; - - // Check if the path specified is a drive - if (PathIsDrive(path)) - { - return PathType.Database; - } - - // chunk the path into parts - string[] pathChunks = ChunkPath(path); - - switch (pathChunks.Length) - { - case 1: - { - string name = pathChunks[0]; - - if (TableNameIsValid(name)) - { - tableName = name; - retVal = PathType.Table; - } - } - break; - - case 2: - { - string name = pathChunks[0]; - - if (TableNameIsValid(name)) - { - tableName = name; - } - - int number = SafeConvertRowNumber(pathChunks[1]); - - if (number >= 0) - { - rowNumber = number; - retVal = PathType.Row; - } - else - { - WriteError(new ErrorRecord( - new ArgumentException("Row number is not valid"), - "RowNumberNotValid", - ErrorCategory.InvalidArgument, - path)); - } - } - break; - - default: - { - WriteError(new ErrorRecord( - new ArgumentException("The path supplied has too many segments"), - "PathNotValid", - ErrorCategory.InvalidArgument, - path)); - } - break; - } // switch(pathChunks... - - return retVal; - } // GetNamesFromPath - - /// - /// Throws an argument exception stating that the specified path does - /// not represent either a table or a row - /// - /// path which is invalid - private void ThrowTerminatingInvalidPathException(string path) - { - StringBuilder message = new StringBuilder("Path must represent either a table or a row :"); - message.Append(path); - - throw new ArgumentException(message.ToString()); - } - - /// - /// Retrieve the list of tables from the database. - /// - /// - /// Collection of DatabaseTableInfo objects, each object representing - /// information about one database table - /// - private Collection GetTables() - { - Collection results = - new Collection(); - - // using ODBC connection to the database and get the schema of tables - AccessDBPSDriveInfo di = this.PSDriveInfo as AccessDBPSDriveInfo; - - if (di == null) - { - return null; - } - - OdbcConnection connection = di.Connection; - DataTable dt = connection.GetSchema("Tables"); - int count; - - // iterate through all rows in the schema and create DatabaseTableInfo - // objects which represents a table - foreach (DataRow dr in dt.Rows) - { - String tableName = dr["TABLE_NAME"] as String; - DataColumnCollection columns = null; - - // find the number of rows in the table - try - { - String cmd = "Select count(*) from \"" + tableName + "\""; - OdbcCommand command = new OdbcCommand(cmd, connection); - - count = (Int32)command.ExecuteScalar(); - } - catch - { - count = 0; - } - - // create DatabaseTableInfo object representing the table - DatabaseTableInfo table = - new DatabaseTableInfo(dr, tableName, count, columns); - - results.Add(table); - } // foreach (DataRow... - - return results; - } // GetTables - - /// - /// Return row information from a specified table. - /// - /// The name of the database table from - /// which to retrieve rows. - /// Collection of row information objects. - private Collection GetRows(string tableName) - { - Collection results = - new Collection(); - - // Obtain rows in the table and add it to the collection - try - { - OdbcDataAdapter da = GetAdapterForTable(tableName); - - if (da == null) - { - return null; - } - - DataSet ds = GetDataSetForTable(da, tableName); - DataTable table = GetDataTable(ds, tableName); - - int i = 0; - foreach (DataRow row in table.Rows) - { - results.Add(new DatabaseRowInfo(row, i.ToString(CultureInfo.CurrentCulture))); - i++; - } // foreach (DataRow... - } - catch (Exception e) - { - WriteError(new ErrorRecord(e, "CannotAccessSpecifiedRows", - ErrorCategory.InvalidOperation, tableName)); - } - - return results; - - } // GetRows - - /// - /// Retrieve information about a single table. - /// - /// The table for which to retrieve - /// data. - /// Table information. - private DatabaseTableInfo GetTable(string tableName) - { - foreach (DatabaseTableInfo table in GetTables()) - { - if (String.Equals(tableName, table.Name, StringComparison.OrdinalIgnoreCase)) - { - return table; - } - } - - return null; - } // GetTable - - /// - /// Obtain a data adapter for the specified Table - /// - /// Name of the table to obtain the - /// adapter for - /// Adapter object for the specified table - /// An adapter serves as a bridge between a DataSet (in memory - /// representation of table) and the data source - private OdbcDataAdapter GetAdapterForTable(string tableName) - { - OdbcDataAdapter da = null; - AccessDBPSDriveInfo di = this.PSDriveInfo as AccessDBPSDriveInfo; - - if (di == null || !TableNameIsValid(tableName) || !TableIsPresent(tableName)) - { - return null; - } - - OdbcConnection connection = di.Connection; - - try - { - // Create a odbc data adapter. This can be sued to update the - // data source with the records that will be created here - // using data sets - string sql = "Select * from " + tableName; - da = new OdbcDataAdapter(new OdbcCommand(sql, connection)); - - // Create a odbc command builder object. This will create sql - // commands automatically for a single table, thus - // eliminating the need to create new sql statements for - // every operation to be done. - OdbcCommandBuilder cmd = new OdbcCommandBuilder(da); - - // Open the connection if its not already open - if (connection.State != ConnectionState.Open) - { - connection.Open(); - } - } - catch (Exception e) - { - WriteError(new ErrorRecord(e, "CannotAccessSpecifiedTable", - ErrorCategory.InvalidOperation, tableName)); - } - - return da; - } // GetAdapterForTable - - /// - /// Gets the DataSet (in memory representation) for the table - /// for the specified adapter - /// - /// Adapter to be used for obtaining - /// the table - /// Name of the table for which a - /// DataSet is required - /// The DataSet with the filled in schema - private DataSet GetDataSetForTable(OdbcDataAdapter adapter, string tableName) - { - Debug.Assert(adapter != null); - - // Create a dataset object which will provide an in-memory - // representation of the data being worked upon in the - // data source. - DataSet ds = new DataSet(); - - // Create a table named "Table" which will contain the same - // schema as in the data source. - //adapter.FillSchema(ds, SchemaType.Source); - adapter.Fill(ds, tableName); - ds.Locale = CultureInfo.InvariantCulture; - - return ds; - } //GetDataSetForTable - - /// - /// Get the DataTable object which can be used to operate on - /// for the specified table in the data source - /// - /// DataSet object which contains the tables - /// schema - /// Name of the table - /// Corresponding DataTable object representing - /// the table - /// - private DataTable GetDataTable(DataSet ds, string tableName) - { - Debug.Assert(ds != null); - Debug.Assert(tableName != null); - - DataTable table = ds.Tables[tableName]; - table.Locale = CultureInfo.InvariantCulture; - - return table; - } // GetDataTable - - /// - /// Retrieves a single row from the named table. - /// - /// The table that contains the - /// numbered row. - /// The index of the row to return. - /// The specified table row. - private DatabaseRowInfo GetRow(string tableName, int row) - { - Collection di = GetRows(tableName); - - // if the row is invalid write an appropriate error else return the - // corresponding row information - if (row < di.Count && row >= 0) - { - return di[row]; - } - else - { - WriteError(new ErrorRecord( - new ItemNotFoundException(), - "RowNotFound", - ErrorCategory.ObjectNotFound, - row.ToString(CultureInfo.CurrentCulture)) - ); - } - - return null; - } // GetRow - - /// - /// Method to safely convert a string representation of a row number - /// into its Int32 equivalent - /// - /// String representation of the row - /// number - /// If there is an exception, -1 is returned - private int SafeConvertRowNumber(string rowNumberAsStr) - { - int rowNumber = -1; - try - { - rowNumber = Convert.ToInt32(rowNumberAsStr, CultureInfo.CurrentCulture); - } - catch (FormatException fe) - { - WriteError(new ErrorRecord(fe, "RowStringFormatNotValid", - ErrorCategory.InvalidData, rowNumberAsStr)); - } - catch (OverflowException oe) - { - WriteError(new ErrorRecord(oe, "RowStringConversionToNumberFailed", - ErrorCategory.InvalidData, rowNumberAsStr)); - } - - return rowNumber; - } // SafeConvertRowNumber - - /// - /// Check if a table name is valid - /// - /// Table name to validate - /// Helps to check for SQL injection attacks - private bool TableNameIsValid(string tableName) - { - Regex exp = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); - - if (exp.IsMatch(tableName)) - { - return true; - } - WriteError(new ErrorRecord( - new ArgumentException("Table name not valid"), "TableNameNotValid", - ErrorCategory.InvalidArgument, tableName)); - return false; - } // TableNameIsValid - - /// - /// Checks to see if the specified table is present in the - /// database - /// - /// Name of the table to check - /// true, if table is present, false otherwise - private bool TableIsPresent(string tableName) - { - // using ODBC connection to the database and get the schema of tables - AccessDBPSDriveInfo di = this.PSDriveInfo as AccessDBPSDriveInfo; - if (di == null) - { - return false; - } - - OdbcConnection connection = di.Connection; - DataTable dt = connection.GetSchema("Tables"); - - // check if the specified tableName is available - // in the list of tables present in the database - foreach (DataRow dr in dt.Rows) - { - string name = dr["TABLE_NAME"] as string; - if (name.Equals(tableName, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } - - WriteError(new ErrorRecord( - new ArgumentException("Specified Table is not present in database"), "TableNotAvailable", - ErrorCategory.InvalidArgument, tableName)); - - return false; - }// TableIsPresent - - #endregion Helper Methods - - #region Private Properties - - private string pathSeparator = "\\"; - private static string pattern = @"^[a-z]+[0-9]*_*$"; - - private enum PathType { Database, Table, Row, Invalid }; - - #endregion Private Properties - } - - #endregion AccessDBProvider - - #region Helper Classes - - #region AccessDBPSDriveInfo - - /// - /// Any state associated with the drive should be held here. - /// In this case, it's the connection to the database. - /// - internal class AccessDBPSDriveInfo : PSDriveInfo - { - private OdbcConnection connection; - - /// - /// ODBC connection information. - /// - public OdbcConnection Connection - { - get { return connection; } - set { connection = value; } - } - - /// - /// Constructor that takes one argument - /// - /// Drive provided by this provider - public AccessDBPSDriveInfo(PSDriveInfo driveInfo) - : base(driveInfo) - { } - - } // class AccessDBPSDriveInfo - - #endregion AccessDBPSDriveInfo - - #region DatabaseTableInfo - - /// - /// Contains information specific to the database table. - /// Similar to the DirectoryInfo class. - /// - public class DatabaseTableInfo - { - /// - /// Row from the "tables" schema - /// - public DataRow Data - { - get - { - return data; - } - set - { - data = value; - } - } - private DataRow data; - - /// - /// The table name. - /// - public string Name - { - get - { - return name; - } - set - { - name = value; - } - } - private String name; - - /// - /// The number of rows in the table. - /// - public int RowCount - { - get - { - return rowCount; - } - set - { - rowCount = value; - } - } - private int rowCount; - - /// - /// The column definitions for the table. - /// - public DataColumnCollection Columns - { - get - { - return columns; - } - set - { - columns = value; - } - } - private DataColumnCollection columns; - - /// - /// Constructor. - /// - /// The row definition. - /// The table name. - /// The number of rows in the table. - /// Information on the column tables. - public DatabaseTableInfo(DataRow row, string name, int rowCount, - DataColumnCollection columns) - { - Name = name; - Data = row; - RowCount = rowCount; - Columns = columns; - } // DatabaseTableInfo - } // class DatabaseTableInfo - - #endregion DatabaseTableInfo - - #region DatabaseRowInfo - - /// - /// Contains information specific to an individual table row. - /// Analogous to the FileInfo class. - /// - public class DatabaseRowInfo - { - /// - /// Row data information. - /// - public DataRow Data - { - get - { - return data; - } - set - { - data = value; - } - } - private DataRow data; - - /// - /// The row index. - /// - public string RowNumber - { - get - { - return rowNumber; - } - set - { - rowNumber = value; - } - } - private string rowNumber; - - /// - /// Constructor. - /// - /// The row information. - /// The row index. - public DatabaseRowInfo(DataRow row, string name) - { - RowNumber = name; - Data = row; - } // DatabaseRowInfo - } // class DatabaseRowInfo - - #endregion DatabaseRowInfo - - #endregion Helper Classes -} -``` - :::code language="csharp" source="~/../powershell-sdk-samples/SDK-2.0/csharp/AccessDBProviderSample03/AccessDBProviderSample03.cs" range="11-976"::: ## See Also From 8ef5ed4c3adda306be8831f5fe3022a93b6d007e Mon Sep 17 00:00:00 2001 From: Sean Wheeler Date: Fri, 5 Nov 2021 15:13:55 -0500 Subject: [PATCH 3/3] Clarify input behaviors for Select-String (#8311) --- .../Select-String.md | 67 ++++++++++++--- .../Select-String.md | 81 +++++++++++++----- .../Select-String.md | 85 ++++++++++++++----- .../Select-String.md | 85 ++++++++++++++----- 4 files changed, 247 insertions(+), 71 deletions(-) diff --git a/reference/5.1/Microsoft.PowerShell.Utility/Select-String.md b/reference/5.1/Microsoft.PowerShell.Utility/Select-String.md index 53f576ec1a3d..e6dc371c596d 100644 --- a/reference/5.1/Microsoft.PowerShell.Utility/Select-String.md +++ b/reference/5.1/Microsoft.PowerShell.Utility/Select-String.md @@ -2,7 +2,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml Locale: en-US Module Name: Microsoft.PowerShell.Utility -ms.date: 10/04/2021 +ms.date: 11/05/2021 online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-string?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: Select-String @@ -41,8 +41,9 @@ Select-String [-Pattern] -LiteralPath [-SimpleMatch] [-Cas ## Description -The `Select-String` cmdlet searches for text and text patterns in input strings and files. You can -use `Select-String` similar to `grep` in UNIX or `findstr.exe` in Windows. +The `Select-String` cmdlet uses regular expression matching to search for text patterns in input +strings and files. You can use `Select-String` similar to `grep` in UNIX or `findstr.exe` in +Windows. `Select-String` is based on lines of text. By default, `Select-String` finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line @@ -50,9 +51,6 @@ containing the match. You can direct `Select-String` to find multiple matches pe before and after the match, or display a Boolean value (True or False) that indicates whether a match is found. -`Select-String` uses regular expression matching, but it can also perform a match that searches the -input for the text that you specify. - `Select-String` can display all the text matches or stop after the first match in each input file. `Select-String` can be used to display all text that doesn't match the specified pattern. @@ -133,10 +131,10 @@ example, the function only exists in the PowerShell session. When the PowerShell the function is deleted. For more information, see [about_Functions](../Microsoft.PowerShell.Core/About/about_Functions.md). ```powershell -Function Search-Help +function Search-Help { - $PSHelp = "$PSHOME\en-US\*.txt" - Select-String -Path $PSHelp -Pattern 'About_' + $PSHelp = "$PSHOME\en-US\*.txt" + Select-String -Path $PSHelp -Pattern 'About_' } Search-Help @@ -313,6 +311,41 @@ line. The objects stored in the `$A` and `$B` variables are identical. The `$B.Matches.Length` property increases because for each line, every occurrence of the pattern **PowerShell** is counted. +### Example 10 - Convert pipeline objects to strings using `Out-String` + +The `ToString()` result of the piped object isn't the same rich string representation produced by +PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. + +Piping to `Out-String` converts the formatted output into a single multi-line string object. This +means that when `Select-String` finds a match it outputs the whole multiline string. + +```powershell +PS> $hash = @{ + Name = 'foo' + Category = 'bar' +} + +# !! NO output, due to .ToString() conversion +$hash | Select-String -Pattern 'foo' + +# Out-String converts the output to a single multi-line string object +PS> $hash | Out-String | Select-String -Pattern 'foo' + +Name Value +---- ----- +Name foo +Category bar + +# Out-String -Stream converts the output to a multiple single-line string objects +PS> $hash | Out-String -Steam | Select-String -Pattern 'foo' + +Name foo +``` + +Piping to `Out-String -String` converts the formatted output into a multipel single-line string +objects. This means that when `Select-String` finds a match it outputs only the matching line. + + ## Parameters ### -AllMatches @@ -639,15 +672,25 @@ the **Quiet** parameter, the output is a Boolean value indicating whether the pa ## Notes -`Select-String` is similar to **grep** in UNIX or **findstr.exe** in Windows. +`Select-String` is similar to `grep` in UNIX or `findstr.exe` in Windows. -The **sls** alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. +The `sls` alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. > [!NOTE] -> According to [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), +> According to +> [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), > the official alias prefix for `Select-*` cmdlets is `sc`, not `sl`. Therefore, the proper alias > for `Select-String` should be `scs`, not `sls`. This is an exception to this rule. +When piping objects to `Select-String`: + +- **FileInfo** objects are treated as a path to a file. When file paths are specified, + `Select-String` searches the contents of the file, not the `ToString()` representation of the + object. +- The `ToString()` result of the piped object isn't the same rich string representation produced by + PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. For + more information, see Example 10. + To use `Select-String`, type the text that you want to find as the value of the **Pattern** parameter. To specify the text to be searched, use the following criteria: diff --git a/reference/7.0/Microsoft.PowerShell.Utility/Select-String.md b/reference/7.0/Microsoft.PowerShell.Utility/Select-String.md index 7494b9456480..a9648fc56c76 100644 --- a/reference/7.0/Microsoft.PowerShell.Utility/Select-String.md +++ b/reference/7.0/Microsoft.PowerShell.Utility/Select-String.md @@ -2,7 +2,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml Locale: en-US Module Name: Microsoft.PowerShell.Utility -ms.date: 10/04/2021 +ms.date: 11/05/2021 online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7&WT.mc_id=ps-gethelp schema: 2.0.0 title: Select-String @@ -65,8 +65,9 @@ Select-String [-Culture ] [-Pattern] -LiteralPath ## Description -The `Select-String` cmdlet searches for text and text patterns in input strings and files. You can -use `Select-String` similar to **grep** in UNIX or **findstr.exe** in Windows. +The `Select-String` cmdlet uses regular expression matching to search for text patterns in input +strings and files. You can use `Select-String` similar to `grep` in UNIX or `findstr.exe` in +Windows. `Select-String` is based on lines of text. By default, `Select-String` finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line @@ -74,9 +75,6 @@ containing the match. You can direct `Select-String` to find multiple matches pe before and after the match, or display a Boolean value (True or False) that indicates whether a match is found. -`Select-String` uses regular expression matching, but it can also perform a match that searches the -input for the text that you specify. - `Select-String` can display all the text matches or stop after the first match in each input file. `Select-String` can be used to display all text that doesn't match the specified pattern. @@ -156,8 +154,8 @@ This example creates a function to search for a pattern in the PowerShell help f example, the function only exists in the PowerShell session. When the PowerShell session is closed, the function is deleted. For more information, see [about_Functions](../Microsoft.PowerShell.Core/About/about_Functions.md). -``` -Function Search-Help +```powershell +function Search-Help { $PSHelp = "$PSHOME\en-US\*.txt" Select-String -Path $PSHelp -Pattern 'About_' @@ -167,10 +165,10 @@ Search-Help ``` ```powershell -C:\Program Files\PowerShell\6\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". -C:\Program Files\PowerShell\6\en-US\default.help.txt:70: Get-Help About_ -C:\Program Files\PowerShell\6\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. -C:\Program Files\PowerShell\6\en-US\default.help.txt:97: about_Updatable_Help +C:\Program Files\PowerShell\7\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". +C:\Program Files\PowerShell\7\en-US\default.help.txt:70: Get-Help About_ +C:\Program Files\PowerShell\7\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. +C:\Program Files\PowerShell\7\en-US\default.help.txt:97: about_Updatable_Help ``` The function is created on the PowerShell command line. The `Function` command uses the name @@ -280,9 +278,9 @@ $A ``` ```Output -C:\Program Files\PowerShell\6\en-US\default.help.txt:3: PowerShell Help System -C:\Program Files\PowerShell\6\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. -C:\Program Files\PowerShell\6\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets +C:\Program Files\PowerShell\7\en-US\default.help.txt:3: PowerShell Help System +C:\Program Files\PowerShell\7\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. +C:\Program Files\PowerShell\7\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets ``` ```powershell @@ -338,6 +336,41 @@ line. The objects stored in the `$A` and `$B` variables are identical. The `$B.Matches.Length` property increases because for each line, every occurrence of the pattern **PowerShell** is counted. +### Example 10 - Convert pipeline objects to strings using `Out-String` + +The `ToString()` result of the piped object isn't the same rich string representation produced by +PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. + +Piping to `Out-String` converts the formatted output into a single multi-line string object. This +means that when `Select-String` finds a match it outputs the whole multiline string. + +```powershell +PS> $hash = @{ + Name = 'foo' + Category = 'bar' +} + +# !! NO output, due to .ToString() conversion +$hash | Select-String -Pattern 'foo' + +# Out-String converts the output to a single multi-line string object +PS> $hash | Out-String | Select-String -Pattern 'foo' + +Name Value +---- ----- +Name foo +Category bar + +# Out-String -Stream converts the output to a multiple single-line string objects +PS> $hash | Out-String -Steam | Select-String -Pattern 'foo' + +Name foo +``` + +Piping to `Out-String -String` converts the formatted output into a multipel single-line string +objects. This means that when `Select-String` finds a match it outputs only the matching line. + + ## Parameters ### -AllMatches @@ -741,7 +774,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ### System.Management.Automation.PSObject -You can pipe any object that has a **ToString** method to `Select-String`. +You can pipe any object that has a `ToString()` method to `Select-String`. ## Outputs @@ -753,15 +786,25 @@ If you use the **Raw** parameter, the output is a set of **String** objects that ## Notes -`Select-String` is similar to **grep** in UNIX or **findstr.exe** in Windows. +`Select-String` is similar to `grep` in UNIX or `findstr.exe` in Windows. -The **sls** alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. +The `sls` alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. > [!NOTE] -> According to [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), +> According to +> [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), > the official alias prefix for `Select-*` cmdlets is `sc`, not `sl`. Therefore, the proper alias > for `Select-String` should be `scs`, not `sls`. This is an exception to this rule. +When piping objects to `Select-String`: + +- **FileInfo** objects are treated as a path to a file. When file paths are specified, + `Select-String` searches the contents of the file, not the `ToString()` representation of the + object. +- The `ToString()` result of the piped object isn't the same rich string representation produced by + PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. For + more information, see Example 10. + To use `Select-String`, type the text that you want to find as the value of the **Pattern** parameter. To specify the text to be searched, use the following criteria: diff --git a/reference/7.1/Microsoft.PowerShell.Utility/Select-String.md b/reference/7.1/Microsoft.PowerShell.Utility/Select-String.md index 38751c4f1d40..a6f82c293756 100644 --- a/reference/7.1/Microsoft.PowerShell.Utility/Select-String.md +++ b/reference/7.1/Microsoft.PowerShell.Utility/Select-String.md @@ -2,7 +2,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml Locale: en-US Module Name: Microsoft.PowerShell.Utility -ms.date: 10/04/2021 +ms.date: 11/05/2021 online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: Select-String @@ -65,8 +65,9 @@ Select-String [-Culture ] [-Pattern] -LiteralPath ## Description -The `Select-String` cmdlet searches for text and text patterns in input strings and files. You can -use `Select-String` similar to **grep** in UNIX or **findstr.exe** in Windows. +The `Select-String` cmdlet uses regular expression matching to search for text patterns in input +strings and files. You can use `Select-String` similar to `grep` in UNIX or `findstr.exe` in +Windows. `Select-String` is based on lines of text. By default, `Select-String` finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line @@ -74,9 +75,6 @@ containing the match. You can direct `Select-String` to find multiple matches pe before and after the match, or display a Boolean value (True or False) that indicates whether a match is found. -`Select-String` uses regular expression matching, but it can also perform a match that searches the -input for the text that you specify. - `Select-String` can display all the text matches or stop after the first match in each input file. `Select-String` can be used to display all text that doesn't match the specified pattern. @@ -156,8 +154,8 @@ This example creates a function to search for a pattern in the PowerShell help f example, the function only exists in the PowerShell session. When the PowerShell session is closed, the function is deleted. For more information, see [about_Functions](../Microsoft.PowerShell.Core/About/about_Functions.md). -``` -Function Search-Help +```powershell +function Search-Help { $PSHelp = "$PSHOME\en-US\*.txt" Select-String -Path $PSHelp -Pattern 'About_' @@ -167,10 +165,10 @@ Search-Help ``` ```powershell -C:\Program Files\PowerShell\6\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". -C:\Program Files\PowerShell\6\en-US\default.help.txt:70: Get-Help About_ -C:\Program Files\PowerShell\6\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. -C:\Program Files\PowerShell\6\en-US\default.help.txt:97: about_Updatable_Help +C:\Program Files\PowerShell\7\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". +C:\Program Files\PowerShell\7\en-US\default.help.txt:70: Get-Help About_ +C:\Program Files\PowerShell\7\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. +C:\Program Files\PowerShell\7\en-US\default.help.txt:97: about_Updatable_Help ``` The function is created on the PowerShell command line. The `Function` command uses the name @@ -280,9 +278,9 @@ $A ``` ```Output -C:\Program Files\PowerShell\6\en-US\default.help.txt:3: PowerShell Help System -C:\Program Files\PowerShell\6\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. -C:\Program Files\PowerShell\6\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets +C:\Program Files\PowerShell\7\en-US\default.help.txt:3: PowerShell Help System +C:\Program Files\PowerShell\7\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. +C:\Program Files\PowerShell\7\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets ``` ```powershell @@ -338,6 +336,41 @@ line. The objects stored in the `$A` and `$B` variables are identical. The `$B.Matches.Length` property increases because for each line, every occurrence of the pattern **PowerShell** is counted. +### Example 10 - Convert pipeline objects to strings using `Out-String` + +The `ToString()` result of the piped object isn't the same rich string representation produced by +PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. + +Piping to `Out-String` converts the formatted output into a single multi-line string object. This +means that when `Select-String` finds a match it outputs the whole multiline string. + +```powershell +PS> $hash = @{ + Name = 'foo' + Category = 'bar' +} + +# !! NO output, due to .ToString() conversion +$hash | Select-String -Pattern 'foo' + +# Out-String converts the output to a single multi-line string object +PS> $hash | Out-String | Select-String -Pattern 'foo' + +Name Value +---- ----- +Name foo +Category bar + +# Out-String -Stream converts the output to a multiple single-line string objects +PS> $hash | Out-String -Steam | Select-String -Pattern 'foo' + +Name foo +``` + +Piping to `Out-String -String` converts the formatted output into a multipel single-line string +objects. This means that when `Select-String` finds a match it outputs only the matching line. + + ## Parameters ### -AllMatches @@ -548,6 +581,9 @@ When you use the **InputObject** parameter to submit a collection of strings, `S the collection as a single combined string. `Select-String` returns the strings as a unit if it finds the search text in any string. +**FileInfo** objects are treated as a path to a file. When file paths are specified, `Select-String` +searches the contents of the file, not the `ToString()` representation of the object. + ```yaml Type: System.Management.Automation.PSObject Parameter Sets: Object, ObjectRaw @@ -746,7 +782,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ### System.Management.Automation.PSObject -You can pipe any object that has a **ToString** method to `Select-String`. +You can pipe any object that has a `ToString()` method to `Select-String`. ## Outputs @@ -758,15 +794,25 @@ If you use the **Raw** parameter, the output is a set of **String** objects that ## Notes -`Select-String` is similar to **grep** in UNIX or **findstr.exe** in Windows. +`Select-String` is similar to `grep` in UNIX or `findstr.exe` in Windows. -The **sls** alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. +The `sls` alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. > [!NOTE] -> According to [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), +> According to +> [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), > the official alias prefix for `Select-*` cmdlets is `sc`, not `sl`. Therefore, the proper alias > for `Select-String` should be `scs`, not `sls`. This is an exception to this rule. +When piping objects to `Select-String`: + +- **FileInfo** objects are treated as a path to a file. When file paths are specified, + `Select-String` searches the contents of the file, not the `ToString()` representation of the + object. +- The `ToString()` result of the piped object isn't the same rich string representation produced by + PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. For + more information, see Example 10. + To use `Select-String`, type the text that you want to find as the value of the **Pattern** parameter. To specify the text to be searched, use the following criteria: @@ -820,4 +866,3 @@ To find the properties of a **MatchInfo** object, type the following command: [Get-WinEvent](../Microsoft.PowerShell.Diagnostics/Get-WinEvent.md) [Out-File](Out-File.md) - diff --git a/reference/7.2/Microsoft.PowerShell.Utility/Select-String.md b/reference/7.2/Microsoft.PowerShell.Utility/Select-String.md index 5e03b5684460..f8d48a464702 100644 --- a/reference/7.2/Microsoft.PowerShell.Utility/Select-String.md +++ b/reference/7.2/Microsoft.PowerShell.Utility/Select-String.md @@ -2,7 +2,7 @@ external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml Locale: en-US Module Name: Microsoft.PowerShell.Utility -ms.date: 10/04/2021 +ms.date: 11/05/2021 online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.2&WT.mc_id=ps-gethelp schema: 2.0.0 title: Select-String @@ -65,8 +65,9 @@ Select-String [-Culture ] [-Pattern] -LiteralPath ## Description -The `Select-String` cmdlet searches for text and text patterns in input strings and files. You can -use `Select-String` similar to **grep** in UNIX or **findstr.exe** in Windows. +The `Select-String` cmdlet uses regular expression matching to search for text patterns in input +strings and files. You can use `Select-String` similar to `grep` in UNIX or `findstr.exe` in +Windows. `Select-String` is based on lines of text. By default, `Select-String` finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line @@ -74,9 +75,6 @@ containing the match. You can direct `Select-String` to find multiple matches pe before and after the match, or display a Boolean value (True or False) that indicates whether a match is found. -`Select-String` uses regular expression matching, but it can also perform a match that searches the -input for the text that you specify. - `Select-String` can display all the text matches or stop after the first match in each input file. `Select-String` can be used to display all text that doesn't match the specified pattern. @@ -156,8 +154,8 @@ This example creates a function to search for a pattern in the PowerShell help f example, the function only exists in the PowerShell session. When the PowerShell session is closed, the function is deleted. For more information, see [about_Functions](../Microsoft.PowerShell.Core/About/about_Functions.md). -``` -Function Search-Help +```powershell +function Search-Help { $PSHelp = "$PSHOME\en-US\*.txt" Select-String -Path $PSHelp -Pattern 'About_' @@ -167,10 +165,10 @@ Search-Help ``` ```powershell -C:\Program Files\PowerShell\6\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". -C:\Program Files\PowerShell\6\en-US\default.help.txt:70: Get-Help About_ -C:\Program Files\PowerShell\6\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. -C:\Program Files\PowerShell\6\en-US\default.help.txt:97: about_Updatable_Help +C:\Program Files\PowerShell\7\en-US\default.help.txt:67: The titles of conceptual topics begin with "About_". +C:\Program Files\PowerShell\7\en-US\default.help.txt:70: Get-Help About_ +C:\Program Files\PowerShell\7\en-US\default.help.txt:93: Get-Help About_Modules : Displays help about PowerShell modules. +C:\Program Files\PowerShell\7\en-US\default.help.txt:97: about_Updatable_Help ``` The function is created on the PowerShell command line. The `Function` command uses the name @@ -280,9 +278,9 @@ $A ``` ```Output -C:\Program Files\PowerShell\6\en-US\default.help.txt:3: PowerShell Help System -C:\Program Files\PowerShell\6\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. -C:\Program Files\PowerShell\6\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets +C:\Program Files\PowerShell\7\en-US\default.help.txt:3: PowerShell Help System +C:\Program Files\PowerShell\7\en-US\default.help.txt:6: Displays help about PowerShell cmdlets and concepts. +C:\Program Files\PowerShell\7\en-US\default.help.txt:9: PowerShell Help describes PowerShell cmdlets ``` ```powershell @@ -338,6 +336,41 @@ line. The objects stored in the `$A` and `$B` variables are identical. The `$B.Matches.Length` property increases because for each line, every occurrence of the pattern **PowerShell** is counted. +### Example 10 - Convert pipeline objects to strings using `Out-String` + +The `ToString()` result of the piped object isn't the same rich string representation produced by +PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. + +Piping to `Out-String` converts the formatted output into a single multi-line string object. This +means that when `Select-String` finds a match it outputs the whole multiline string. + +```powershell +PS> $hash = @{ + Name = 'foo' + Category = 'bar' +} + +# !! NO output, due to .ToString() conversion +$hash | Select-String -Pattern 'foo' + +# Out-String converts the output to a single multi-line string object +PS> $hash | Out-String | Select-String -Pattern 'foo' + +Name Value +---- ----- +Name foo +Category bar + +# Out-String -Stream converts the output to a multiple single-line string objects +PS> $hash | Out-String -Steam | Select-String -Pattern 'foo' + +Name foo +``` + +Piping to `Out-String -String` converts the formatted output into a multipel single-line string +objects. This means that when `Select-String` finds a match it outputs only the matching line. + + ## Parameters ### -AllMatches @@ -548,6 +581,9 @@ When you use the **InputObject** parameter to submit a collection of strings, `S the collection as a single combined string. `Select-String` returns the strings as a unit if it finds the search text in any string. +**FileInfo** objects are treated as a path to a file. When file paths are specified, `Select-String` +searches the contents of the file, not the `ToString()` representation of the object. + ```yaml Type: System.Management.Automation.PSObject Parameter Sets: Object, ObjectRaw @@ -746,7 +782,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ### System.Management.Automation.PSObject -You can pipe any object that has a **ToString** method to `Select-String`. +You can pipe any object that has a `ToString()` method to `Select-String`. ## Outputs @@ -758,15 +794,25 @@ If you use the **Raw** parameter, the output is a set of **String** objects that ## Notes -`Select-String` is similar to **grep** in UNIX or **findstr.exe** in Windows. +`Select-String` is similar to `grep` in UNIX or `findstr.exe` in Windows. -The **sls** alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. +The `sls` alias for the `Select-String` cmdlet was introduced in PowerShell 3.0. > [!NOTE] -> According to [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), +> According to +> [Approved Verbs for PowerShell Commands](/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands), > the official alias prefix for `Select-*` cmdlets is `sc`, not `sl`. Therefore, the proper alias > for `Select-String` should be `scs`, not `sls`. This is an exception to this rule. +When piping objects to `Select-String`: + +- **FileInfo** objects are treated as a path to a file. When file paths are specified, + `Select-String` searches the contents of the file, not the `ToString()` representation of the + object. +- The `ToString()` result of the piped object isn't the same rich string representation produced by + PowerShell's formatting system. So, you may need to pipe the objects to `Out-String` first. For + more information, see Example 10. + To use `Select-String`, type the text that you want to find as the value of the **Pattern** parameter. To specify the text to be searched, use the following criteria: @@ -820,4 +866,3 @@ To find the properties of a **MatchInfo** object, type the following command: [Get-WinEvent](../Microsoft.PowerShell.Diagnostics/Get-WinEvent.md) [Out-File](Out-File.md) -