Posts

Showing posts with the label pwsh

Sorting properties (not objects) in PowerShell

Image
This week I had a need for sorting the properties of an object alphabetically. Funny enough, the aptly named "Sort-Object" does not do this, that cmdlet sorts an array of objects by one or more given property names – useful for listing files in a folder, ordered by the files' size, for example. But I had to figure out something else. Take this example, just all the properties of a file in default order. Say I want to sort these alphabetically. First, I need to identify exactly which properties. Get-Member gives you the methods in addition to the properties, I don't want the methods. I just want the different properties, I got those with -MemberType *Property. I want the properties sorted by Name, and then expand the Name. Now, store that in a variable, here named $SortedProps. This variable is now a string array. Now, back to start with the Get-Item of the file. That output gets piped to Select-Object, which has the parameter "Property" that takes a string a...

Automated Grocery Shopping

Image
I want to share a project I worked on a while ago, that I still use from time to time.  Basically all my grocery shopping is done at Kolonial.no, which is a Norwegian online grocery store that has an api for the shopping cart and products.  I am a cheapskate in many fields, including grocery shopping. And how I shop is somewhat robotic, very predictable and some might say boring and uninspired. But the thought process when I select items, can easily be automated, so I did. The code is on my github .  All the docs are there, so I won't bore you with that here.  But the idea and process is quite simple: I need eggs. Eggs are not a product. Eggs are a category of products . So my shopping list in this code is the category ID of eggs at kolonial.no.  On the same line in my shopping list I can also specify words that should or should not appear in the product name. For example "frittgående" should be in the name. (Norwegian for "free range") Next filter is whether or...