Let's say, you want your non-handout-document as filename.pdf and you want your handout-document as filename_handout.pdf.
Maintain both versions via the .tex-source file filename.tex but compile filename.tex only for obtaining the non-handout-document whose name will be filename.pdf:
% \providecommand does define a command only in case it is
% not already defined.
\providecommand\Handoutmode{01}%<-Not in Handout-Mode
\newcommand\Handoutfork{}%
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname
\if\Handoutmode
% load whatever you need for the handout but don't need for
% the main document:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
% load whatever you need for the main document nut not for
% the main document:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi
\begin{document}
You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.
This is text that is both in the main document and in the handout.
\Handoutfork{This is Text that goes into the handout only.}%
{This is Text that goes into the main document only.}
This is text that is both in the main document and in the handout.
\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi
This is text that is both in the main document and in the handout.
\end{document}
For obtaining the handout as filename_handout.pdf maintain in the same folder/directory and compile another .tex-input file whose name is filename_handout.tex and whose content consists of two lines:
\providecommand\Handoutmode{00}%<-In Handout-Mode
\input filename.tex
With the two examples, filename.pdf will be:

And filename_handout.pdf will be:

Of course you can automatize the creating of filename_handout.pdf without an extra filename_handout.tex via \write18-calls to filename.pdf with option
--job-name=\jobname_handout (MiKTeX) respective
--jobname=\jobname_handout (TeXLive).
When doing this, you can, e.g., evaluate whether the expansion of \jobname has a trailing phrase _handout and define your handout-forking-macros accordingly.
I did not see this proposition of evaluating \jobname in the other threads.
By the way:
As with each variant of the document you might need several LaTeX-runs for compiling it, while it isn't ensured that with each variant of the document you need the same amount of LaTeX-runs for compiling it, I would, in case of MiKTeX being available, not call the pdflatex -program but the texify-program with the --pdf-option and with passing on to the pdflatex-program the option --job-name=\jobname_handout via texify's --tex-option as
--tex-option="--job-name=\jobname_handout".
I would do that \write18-call to texify only during the first LaTeX-run on filename.tex when not in handout-mode.
Detecting the first LaTeX run can probably be done by checking the existence of the aux-file(s).
Here comes the code.
(If you wish, you can put some of the code into a package which you can load via \RequirePackage or via \input before forking what documentclass to use etc.)
%--------------------------------------------------------------------
% The following lines could go into a separate package that is to be
% loaded via \input or (in case of also having package options)
% via \RequirePackage before writing any other preamble-thinglie like
% \documantclass...
%--------------------------------------------------------------------
% Check whether the current \jobname has a trailing phrase "_handout"
% and set \Handoutmde accordingly.
\begingroup
\makeatletter
%
\def\testA{_handout}%
\@onelevel@sanitize\testA
%
\expandafter\long\expandafter\def\expandafter\testB
\expandafter#\expandafter1\testA{}%
%
\edef\testA{%
\expandafter\expandafter
\expandafter \testB
\expandafter\jobname
\testA
}%
\def\testB{}%
\ifx\testA\testB
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\endgroup\providecommand\Handoutmode{01}}%<-Not in Handout-Mode
{\endgroup\providecommand\Handoutmode{00}}%<-In Handout-Mode
\newcommand\Handoutfork{}%
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname
% (If you put the code into a package by itself, you can create a
% package option for deciding whether to create or not to create
% the handout.)
%
% Create the handout:
\if\Handoutmode\else
% TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
% \immediate\write18{pdflatex --jobname=\jobname_handout \jobname.tex}%
% MiKTeX-Variant, here the option is --job-name.:
\immediate\write18{pdflatex --job-name=\jobname_handout \jobname.tex}%
% With MiKTeX better might be:
% \IfFileExists{\jobname.aux}{}{%
% \immediate\write18{texify --pdf --clean --run-viewer --tex-option="--job-name=\jobname_handout" \jobname.tex}%
% }%
\fi
%--------------------------------------------------------------------
% From here on, things cannot go into a separate package any
% more.
%--------------------------------------------------------------------
\if\Handoutmode
% load whatever you do need for the handout but do not need for
% the main document:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
% load whatever you do need for the main document but do not need for
% the main document:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi
\begin{document}
You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.
This is text that is both in the main document and in the handout.
\Handoutfork{This is Text that goes into the handout only.}%
{This is Text that goes into the main document only.}
This is text that is both in the main document and in the handout.
\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi
This is text that is both in the main document and in the handout.
\end{document}
A starting point for a separate package AddHandout as mentioned above could be something like this—I decided to give the package a bizarre name because I do not intend to develop/maintain it any further and therefore I did not want to block a "good" package name:
AddHandoutBizarrePackageName.sty:
%% This is file `AddHandoutBizarrePackageName.sty',
%%
%% Copyright (C) 2018 by Ulrich Diez <ud.usenetcorrespondence@web.de>
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of LaTeX version
%% 2003/12/01 or later.
%%
%% This file forms a "work" according to the terms of the
%% LaTeX Project Public License.
%%
%% This "work" is not maintained.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{AddHandoutBizarrePackageName}[2018/08/23 V.0.1beta -- A \write18-hook for handout-compilation]
\RequirePackage{kvoptions}
% Check whether the current \jobname has a trailing phrase "_handout"
% and set \Handoutmde accordingly.
\begingroup
%
\def\testA{_handout}%
\@onelevel@sanitize\testA
%
\expandafter\long\expandafter\def\expandafter\testB
\expandafter#\expandafter1\testA{}%
%
\edef\testA{%
\expandafter\expandafter
\expandafter \testB
\expandafter\jobname
\testA
}%
\def\testB{}%
%
\ifx\testA\testB
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\endgroup\providecommand\Handoutmode{01}}%<-Not in Handout-Mode
{\endgroup\providecommand\Handoutmode{00}}%<-In Handout-Mode
%
\newcommand\Handoutfork{}%
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname
%
\SetupKeyvalOptions{family=\@currname, prefix=\@currname @, setkeys=\kvsetkeys}
\DeclareStringOption[no_handout]{platform}[]%
\DeclareDefaultOption{\@unknownoptionerror}%
\ProcessLocalKeyvalOptions{\@currname}%
\begingroup
\def\testA{Handout-MiKTeX-texify}%
\ifx\testA\AddHandoutBizarrePackageName@platform
\if\Handoutmode\else
\IfFileExists{\jobname.aux}{}{%
\immediate\write18{texify --pdf --clean --run-viewer --tex-option="--job-name=\jobname_handout" \jobname.tex}%
}%
\fi
\else
\def\testA{Handout-MiKTeX}%
\ifx\testA\AddHandoutBizarrePackageName@platform
\if\Handoutmode\else
\immediate\write18{pdflatex --job-name=\jobname_handout \jobname.tex}%
\fi
\else
\def\testA{Handout-TeXLive}%
\ifx\testA\AddHandoutBizarrePackageName@platform
\if\Handoutmode\else
\immediate\write18{pdflatex --jobname=\jobname_handout \jobname.tex}%
\fi
\else
\def\testA{no_handout}%
\ifx\testA\AddHandoutBizarrePackageName@platform
\else
\def\testA{}%
\ifx\testA\AddHandoutBizarrePackageName@platform
\PackageWarningNoLine{\@currname}{%
You need tp specify a value for the option "platform".
Handout will not be created%
}%
\else
\PackageWarningNoLine{\@currname}{%
The platform '\AddHandoutBizarrePackageName@platform'
is unknown. Handout will not be created%
}%
\fi
\fi
\fi
\fi
\fi
\endgroup
\endinput
You can load the package via \RequirePackage before writing any other component
of the preamble.
For forking depending on whether currently producing the handout or the main document,
\if\Handoutmode .. \else .. \fi
and
\Handoutfork{This is Text that goes into the handout only.}%
{This is Text that goes into the main document only.}%
are available.
If you load the package without any option, only the file ⟨filename⟩.pdf but no no file ⟨filename⟩_handout.pdf will be produced.
If you load the package with option "platform=Handout-MiKTeX-texify", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the texify-program of the TeX-Distribution "MiKTeX".
If you load the package with option "platform=Handout-MiKTeX", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the pdflatex-program of the TeX-Distribution "MiKTeX".
If you load the package with option "platform=Handout-TeXLive", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the pdflatex-program of the TeX-Distribution "TeXLive".
This way you can within one single \RequirePackage-line of the document specify if you wish to also obtain a handout.
Besides this you have facilities for both in the preamble and in the document forking depending on whether currently the main document or the handout is typeset.
Usage-example, e.g., filename.tex
% Produce only <filename>.pdf, no <filename>_handout.pdf:
%\RequirePackage{AddHandoutBizarrePackageName}
% Produce only <filename>.pdf, no <filename>_handout.pdf:
%\RequirePackage[platform=no_handout]{AddHandoutBizarrePackageName}%
% Produce <filename>.pdf and try to produce <filename>_handout.pdf
% via \write18 calls suitable for MiKTeX' texify:
\RequirePackage[platform=Handout-MiKTeX-texify]{AddHandoutBizarrePackageName}
% Produce <filename>.pdf and try to produce <filename>_handout.pdf
% via \write18 calls suitable for MiKTeX' pdflatex:
%\RequirePackage[platform=Handout-MiKTeX]{AddHandoutBizarrePackageName}
% Produce <filename>.pdf and try to produce <filename>_handout.pdf
% via \write18 calls suitable for TeXLive' pdflatex:
%\RequirePackage[platform=Handout-TeXLive]{AddHandoutBizarrePackageName}
\if\Handoutmode
% load whatever you do need for the handout but do not need for
% the main document:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
% load whatever you do need for the main document but do not need for
% the handout:
\documentclass{article}
\newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi
\begin{document}
You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.
This is text that is both in the main document and in the handout.
\Handoutfork{This is Text that goes into the handout only.}%
{This is Text that goes into the main document only.}
This is text that is both in the main document and in the handout.
\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi
This is text that is both in the main document and in the handout.
\end{document}
In any case you use the same .tex-input-code for creating different variants of a document.
Be aware that you need to pay special attention in case the .tex-input-code is also used for having something done where randomness plays a role.
E.g., having via \pgfrandom created some numbers at random and henceforth having LaTeX use these random numbers within the document that is about to be created.
When compiling the one variant of the document, the random-number-generator might produce random values that differ from those produced by the random-number-generator when compiling the other variant of the document. Thus in such a scenario the content of the two documents might not be identical any more. ...