Skip to main content

r/PowerShell


Stop using [System]
Stop using [System]
Script Sharing

I'm getting old enough that my fingers hate my lifetime of programming.

I'll save a few keystrokes where I can.

There's something simple most people don't seem to know about PowerShell syntax.

It saves seven characters of typing every you use this, and runs a tiny bit faster.

You never need to specify stuff is in the [System] namespace.

Stop Using [System]

.NET is a huge framework with tons of useful stuff in it. There's a lot of stuff in the System namespaces. Built-in framework functionality often exists in one of the many namespaces in System.

By the time PowerShell was being built, it was pretty clear that leveraging .NET was worth it, and that most people wouldn't want to type six to seven more characters every time.

So, since PowerShell v1, you haven't had to.

You can omit the [System] in any type in any system namespace

So instead of:

 [system.collections.generic.list[string]]

We can write:

 [collections.generic.list[string]]

Instead of:

 [System.Collections.IDictionary]

We can write:

 [Collections.IDictionary]

This is true for every system type. On my machine, there are 4722 public types in the system namespace. That's 33054 characters I will never have to type.

It makes scripts shorter and simpler to read.

Also, when PowerShell resolves types, it checks for the shorter names first. This saves a very tiny amount of time in each of your scripts. (I was corrected)

Yet, sadly, I see the system namespace everywhere in people's scripts.

I beg of you all:

  • Save your fingers

  • Make scripts shorter

Stop Using [System]


Advertisement: Global business means global invoices. Remitly Business helps you pay them quickly and on time.
Global business means global invoices. Remitly Business helps you pay them quickly and on time.
Global business means global invoices. Remitly Business helps you pay them quickly and on time.


Simple Splatting and the GitHub CLI
Simple Splatting and the GitHub CLI
Script Sharing

Brevity may be the soul of wit, and I may be bad at it.

Let's try to make a quick post about a little daily PowerShell timesaver: splatting the GitHub CLI.

I'm going to show you how you can save typing and time with the GitHub CLI.

What is Splatting?

Splatting is a simple technique in PowerShell. It lets you pass multiple parameters. It's been there since PowerShell version 2. This is old, consistent technique.

Most people are used to splatting a dictionary, like:

# You can splat a dictionary
$MyId = @{id=$pid}
Get-Process @MyId

This is a cool and useful technique, and most people overlook the other half of splatting:

# You can splat a list
$allIssues = @('--state', 'all', '--limit, '2kb')
gh issue list @allIssues

The Trick

You just saw it.

There are millions of apps you could use this trick with.

I just happen to use the github cli on most days.

You're trading typing all of those arguments for typing a shorter string.

It saves seconds every time you use it.

And it's one simple line.

You can stick it in your profile and every time you're using PowerShell, you'll have those variables to use.

You can just type this sort of trick in if you find yourself using the same parameters.

Think of it as a preset of parameters. Because that's basically what it is.

Multiple Splats

It's important to know that you can do multiple splats.

Here's a simple example:

$bug = @('--label', 'bug')
$assignMe = @('--assignee', '@me')

gh issue create --title 'Some Issue' --body 'Some problem' @assignMe @bug

This would create a bug.

Let's make another, longer one:

# Get issue information as json.
$IssueAsJson= @('--json', (
    'assignees','author','body','closed','closedAt',
    'closedByPullRequestsReferences','comments','createdAt',
    'isPinned','labels','milestone','number','reactionGroups',
    'state','stateReason','title','updatedAt','url' -join ','    
))

$allIssues = @('--state', 'all', '--limit, '2kb')
$IssueList = gh issue list @allIssues @issueAsJson | ConvertFrom-Json
$issueList

If you run this, you

  • List all of the issues from a repo as json

  • Convert them from json into objects

Feel free to copy/paste these tricks into your profile. They're handy!


Try to set the Org SharePoint Sharing Link Retention
Try to set the Org SharePoint Sharing Link Retention
Question

As per title really I want to set a few values for these settings

Set-SPOTenant -CoreOrganizationSharingLinkMaxExpirationInDays 365

Set-SPOTenant -OneDriveOrganizationSharingLinkMaxExpirationInDays 365

And a couple for Link recommendations as an example.

Questions:

    1. Is SPO still active? And if yes, should I install the module and use that over PNP?

  • 1a) I thought I read it was going to be removed shortly.

  • I have PNP installed correctly and can log in to it, so can I set this with PNP?

  • 2) I tried Set-PNPTenant -CoreOrganizationSharingLinkMaxExpirationInDays 365 and it throws an error for the Set-PNPTenant

So I'm not sure if it's possible with PNP.

  • 3) Also If I use Get-SPOTenant and Get-PNPTenant are they settings the same?

Im very amateur at code, so quite possibly I'm doing it wrong.

Thoughts?