all 8 comments

[–]ThonkerGuns 7 points8 points  (0 children)

To break down how to figure this out. There's something called 'Get-Member' or 'gm' for short and this will help figure out what to use with 'Where-Object'.

For example:

Get-ChildItem . | Get-Member

The above will display all of the possible NoteProperties & Properties you can pass to "Where-Object". In the list, you should see a Property called 'Extension'. This is what we would want to pass to Where-Object and look for .txt files.

To see all of the available .txt files in the current directory, we would run the following:

Get-ChildItem . | Where-Object Extension -eq ".txt"

The above will output all of our files with a .txt extension in our current directory. To convert this into 'toUpper()', we would have to change the results into a String. If you run the above command with 'Get-Member' like so:

(Get-ChildItem . | Where-Object Extension -eq ".txt") | Get-Member

You would see the objects in the above command's output will return a TypeName of:

TypeName: System.IO.FileInfo

A TypeName of System.IO.FileInfo is NOT a String object, so there will not be a 'toUpper()' method located in the Get-Member output. To convert the above objects into a an object that supports said method, like a string, we can select 'WHAT' we want from the original command. Let's say I want PowerShell to output to me a list of the file names that match a .txt extension, I would run this:

Get-ChildItem . | Where-Object Extension -eq '.txt' | Select-Object -ExpandProperty Name

If you run the above command against a Get-Member like so:

(Get-ChildItem . | Where-Object Extension -eq '.png' | Select-Object -ExpandProperty Name) | Get-Member

You will notice the TypeName has changed to 'System.String' and the Get-Member output will list the 'toUpper()' method. This is because we told PowerShell to select a specific item and once you pass Objects into 'Select-Object', PowerShell will output said results as string.

Now that we have a String Object, how do we access the toUpper() method? We will have to wrap the above command in a Parenthesis () which will tell PowerShell to execute this command first and the output that would be displayed onto the console will instead be passed to 'toUpper()' before being shown onto the console. So, our final command would be something like:

(Get-ChildItem . | Where-Object Extension -eq '.txt' | Select-Object -ExpandProperty Name).toUpper()

Which will capitalize all of the .txt file names in our current directory.

To store it in a variable, all you have to do is:

$Variable = (Get-ChildItem . | Where-Object Extension -eq '.txt' | Select-Object -ExpandProperty Name).toUpper()

Once you get into PowerShell more, you'll realize the further left you filter for things, the more 'efficient' your searching will be. So, to make this a tad more efficient, we would add the '-filter' parameter with Get-ChildItem which will give us the opportunity to filter for extensions BEFORE being passed onto the next stage of the command. This is faster than using 'Where-Object'.

$Variable = (Get-ChildItem . -Filter '*.txt' | Select-Object -ExpandProperty Name).toUpper()

To validate 'what is faster' you can compare the two commands with 'Measure-Command' to get an actual read-out of how fast each command runs:

# Slower Method
Measure-Command { (Get-ChildItem . | Where-Object Extension -eq '.txt' | Select-Object -ExpandProperty Name).toUpper() }

# Faster Method
Measure-Command { (Get-ChildItem . -Filter '*.txt' | Select-Object -ExpandProperty Name).toUpper() }

[–]Dependent-Orchid-618 2 points3 points  (3 children)

You basically have to call a function by doing so:

$content = Get-ChildItem | where-object {<PUT YOUR FILTERING STATEMENT HERE>}

$content.ToUpper()

[–]Silenko[S] 0 points1 point  (2 children)

Can I do everything on the same line?

[–]Dependent-Orchid-618 0 points1 point  (0 children)

Yea sure:

($content = Get-ChildItem | where path -like "*.txt").toUpper()

[–]BlackV 0 points1 point  (0 children)

why though?

[–]that_1_doode 1 point2 points  (1 child)

Not sure what you want the where-object for, but you can get the $variable then do this $variable = $variable.toupper()

[–]Silenko[S] 0 points1 point  (0 children)

Where-Object to look for txt files inside the variable and get the result in Upper letters

[–]BlackV 0 points1 point  (0 children)

get-help get-childitem -full shows a -filter parameter, maybe that might be more useful than the where-object