Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist. Författare till bok om C64 och senbliven lantis. Röstar pirat.

Startsidan > Texter > Teknikblogg

Category: PowerShell

Objektnotation: XML, JSON, PSON

Här följer en kort kommentar om tre olika format för textbaserad objektnotation. XML Fördelar: Stöd för dokumenttypsdefinitioner (DTD) och scheman. God tillgång på bra API:er.Nackdelar: Mycket overhead (ett "pratigt" språk), endast Visual Basi [...]

Stora tal

Det finns ett mycket roligt klipp med karaktären Ali G där han ställer sig frågan om en dator någonsin kan räkna ut ett väldigt stort godtyckligt tal. Han får svaret att alla räkneövningar han kan nämna, kan räknas ut av en modern dator. Din miniräkn [...]


Läs gärna min text om Loernz-attraktionen, med tillhörande PowerShell-implementation, på Humanistbloggen! Uppdatering 2015-01-02: Jonas Elfström har översatt PowerShell-koden till Swift.

Anropa en web service från PowerShell

För att sätta en servicereferens inne i Visual Studio måste man (numera) följa dessa steg: 1. Högerklicka på References i Solution Explorer, välj Add Service Reference. 2. Klicka på knappen Advanced. 3. Klicka på knappen Add Web Reference. [...]

Play some music COM music

PowerShell can make use of the .NET Framework, and can load assemblies using the Load function in System.Reflection.Assembly. Also, PowerShell can also use any COM class that is registered on your system. This feature gives you the ability to automat [...]

Declaring multiple variables

In PowerShell, variables are created when they are first used. Just by assigning a value to a variable, that variable is created. $x = 4 If you assign a different kind of value to that same variable, the type of the variable is changed. $x = [...]

Reading out SQL Data with named columns

This code reads out names from the Employees table of the Northwind database. You must correct the Data Source property in the connection string for it to run. #Create a connection object and open it. [String]$cns="Data Source=XXX;Initial Catal [...]

The shortest code you need to draw vector graphics

This example shows the shortest code you need to draw vector graphics in PowerShell. However, this code demonstrates a problem rather than a solution. The .NET Framework wants you to inherit a Form class when you describe a picture, because you will [...]

Piping objects

If you want to create and run a ps1 script, make sure to give yourself the permission to execute scripts. If you are running a newer operating system, you must start PowerShell as an administrator the time that you grant yourself the right to run scr [...]

PowerShell functions argument catching

PowerShell is not very object oriented. The support for creating classes is implemented through the Add-Type cmdlet that takes C# code. Here I show how to use Add-Type to define and expose classes that use any part of the .NET Framework. These classe [...]

PowerShell knows about your USB devices

One of many things you can do with the Management assembly is to get the USB devices that is currently connected to your computer. To load the Management assembly, use this line of code (no line break here): [System.Reflection.Assembly]::Load("Sys [...]

Adding references to custom types

In a great number of blog posts, I have used the Load function of System.Reflection.Assembly to load types from the .NET Framework. This (without line break) is an example that gives you access to the Forms library... [System.Reflection.Assembly]: [...]

Calling C functions

Like I mentioned earlier, functions that only are exposed through an external .NET interface are unreachable from PowerShell, but can be called using reflection. But how about C functions? This is not a problem at all. Like always when you att [...]

Create an exe using PowerShell

This is, from what I know, the lest code you will need to write, to make PowerShell create a exe-file that do what you want it to do. It uses the VB compiler that is installed together with the .NET Framework together with the VB-specific CreateCompi [...]

Visual Basic/PowerShell interaction

Through the .NET Framework, Visual Basic can talk to PowerShell. If PowerShell is installed, you can add a reference to an assembly named System.Management.Automation. You will have to browse for it, and you will find it here: C:\Program Files\Ref [...]

Easy archiving of files using PowerShell

This example shows easy compressing and archiving of files using the WindowsBase library and PowerShell. This code will use the WindowsBase library to create a ZIP archive from a couple of images. In this example, I am working in the C root, and the [...]

Reacting to user input

Continued from here. The concept of events is that when a GUI component (a "control") reacts on something like user actions, the control has the power to inform anyone who wants to know about that action. This is the event system of the .NET F [...]

Using the Graphical User Interface

If you are doing this as a script, you must have given yourself the permission execute scripts. To start, you must load the library that holds the classes that are used for GUI programming. Remember, you cannot do a line break here. I only do i [...]

Getting information

The cmdlet for finding out what capabilities (members) an object have, is called Get-Member. You can pass any object to this cmdlet, and you can filter its return. This will give all members of a regular string object: Get-Member -InputObject "Hel [...]

For Each

The ForEach keyword is used to iterate through a collection of objects that you have a reference to. You can get a reference to a collection by calling a function or by declaring and initializing an array. This example will give you an array of strin [...]

There is nothing that can’t be done with PowerShell

Basically, what makes PowerShell a so much better command line shell and scripting language than its competitors, is that you can do anything and everything. If you want to create scripts, you must give yourself permission to do so (shown here). In a [...]

Checking a twitter feed using PowerShell

To check a twitter feed using PowerShell, you must have an URL to an XML feed (probably RSS). Open the web site of the account you're interested in ( and find the RSS link. Mine is [...]

PowerShell and Windows 7

I should have posted this long ago, but here it is. I did not manage to install PowerShell 2.0 on Windows 7. Windows complained about a newer version that already was supposed to exist on the system. And yes, just open the Run dialog (Win+R) and type [...]

How to download a file, the PowerShell way

If you want to try this out, please read this first. There, I did an upload using FTP with network credentials (username and password). This time, I want to show how to download a file using HTTP. I am using PowerShell 2.0. #Load the System.Ne [...]

Calling a function

The parenthesis are important when you are calling a function in PowerShell. The parenthesis tells PowerShell that you intend to call the method, not acquire a reference to it. This code illustrates this. $random = New-Object System.Random $res [...]

How to upload a file, the PowerShell way

If you haven't activated scripting in PowerShell, do that first. To access the System.Net namespace, I have to load it using the Load method (System.Reflection.Assembly.Load). To know what version and public key you must refer to, check the Sy [...]

The splat operator

I must admit that I am not completely up to date with PowerShell 2.0 (PS2), even though the CTP (community technology preview) has been out for a while. The first thing that you will notice, is that you are no longer bound to the old console window. [...]

Execute a cmdlet or PS1 script from Visual Basic

The cmdlets in PowerShell can easily be executed from within Visual Basic. I have done this in a few Visual Basic 8 projects, and the code for doing this from Visual Basic 8 can be somewhat ugly, but in Visual Basic 9 (and .NET Framework 3.5) it is v [...]

Type safety in PowerShell

Good or bad, here are some attributes of the PowerShell type system: Variables are declared when they are first used. Just assign a value to a variable, and the variable is created if it doesn’t exist. The variables are not type safe. Try typing $ [...]

Download PowerShell and activate scripting

To install Microsoft PowerShell version 1.0, you must download the correct package for your version of Windows from Microsoft Download Center. Some of the packages require that you validate your copy of Windows.  Windows Vista Windows XP Windows S [...]

Defining and calling functions

You can define your own functions in a PowerShell script file (PS1-file) or directly from the PowerShell console. To do this, use the keyword function, followed by the definition encapsulated in curly brackets. This code creates a function that r [...]

Using the .NET Framework from PowerShell

If you want to call a static function (Shared in Visual Basic) in the .NET Framework, you type in the full name of the class (including the namespaces) followed by double colon (::) and the name of the function you want to call. The function L [...]

Getting started, part 3 of 3

About providers and cmdlets: If you just start PowerShell and use these cmdlets, they are for file management. The Add-Content cmdlet can create a file if it doesn’t exist, and append information to that file. If you execute the following line, yo [...]

Getting started, part 2 of 3

When you know about cmdlets and how to get help on them, the next thing is to learn how to use the window.All commands you type in are saved in the command buffer. The keys you use to access the buffer is: - [Up] for the previous command in the bu [...]

Getting started, part 1 of 3

First of all, your old commands that you used in cmd.exe still work. You have to be a tiny bit stricter in syntax. For example, when you navigate to the parent folder in cmd.exe, you could write: cd.. In PowerShell, you have to add a space [...]

En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!


Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.