The Powershell Way? Design everything as a cmdlet that performs one task?

Hello all,

I am currently using Powershell Studio and I am attempting to write a program to connect to using Powershell - the problem I have is I do not know if it is possible to make everything a cmdlet, for example:

function Get-TWAuthHeaders($APIKey)
$bytes = [System.Text.Encoding]::ASCII.GetBytes($APIKey)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
@{ Authorization = $basicAuthValue } # Return This Object

This is just a function to capture the authentication headers that are used for connection when interacting with an API, I cannot possibly make this into a cmdlet as it does not perform anything as such - is this acceptable or am I ‘breaking’ ‘The Powershell Way’ of doing things?

Thanks for any advice

I’m not sure I understand your question but perhaps PowerShell “Modules” will help you.

Well I’m not sure what your view of a cmdlet and a function is but here is one description:

With that said it kind of depends on what you intend to do.
The code in your post suggest that this is more of a helper function.
Something that is not intended for the user to consume but something that is internal to the script or module.
E.g. a poor comparison would be private vs. public classes/methods in other languages.

There is nothing wrong to break out code into smaller functions.
I would say that it’s the prefered way, as long as it make sense.
E.g. if it’s one line of code it’s probably not worth writing a whole function for it.

Without seeing the rest of the code it will be difficult to say if it make sense or not in your case.
E.g. maybe the function that retrieves the data from the API could include those steps rather than a seperate function.
But basically you have to make the judgement call on what is right for what you want to do.

But again, there is no specific powershell way, as far as I know, that says that you shouldn’t break out code into smaller pieces.

Thanks very much for the responses - kind of leaves me wondering whether I should use C# or Powershell.

Well it depends on what you need it to do.
Both C# and Powershell uses .Net so you have the same framework to work with.

But of course there are things that will prefer one or the other.
E.g. if you’re creating a api service then you would use something like C# or e.g. Python.
Or if you want to create a more complex GUI tool then C# would be prefered.

If you’re doing e.g. more admin type tools then you’ll most likely do it quicker in powershell.
Or a combination if you want to go that route.

But in the end it also comes down to what you’re most familiar with.
Powershell have a much lower learning curve compared to C#.